Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
Delete items with event

I need to delete all my items except Select value from menu id when the user clicks above a dropdown menu, for that I did an event.

This what I tried:

const menu = document.querySelector("#menu");

menu.addEventListener("click", () => {
  let sonsLength = menu.length;

  for (let i = 0; i < sonsLength; i++) {
    if (menu[i].value != "Select") {
      menu.removeChild(menu[i]);
      sonsLength = sonsLength - 1;
    }
  }
})
<select id="menu">
  <option value="Select">Select</option>
  <option value="Madrid">Madrid</option>
  <option value="Barcelona">Barcelona</option>
  <option value="Sevilla">Sevilla</option>
  <option value="Bilbao">Bilbao</option>
  <option value="Manchester">Manchester</option>
  <option value="Paris">Paris</option>
</select>

However my loop it doesn't work how I supposed, because the user needs to click several times above the dropdown menu to delete all items except the first one and because of my understanding it should be delete the items when the user clicks once.

Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I am still not sure if this is what you want to achieve but maybe I am right?

const menu = document.querySelector("#menu");

menu.addEventListener("change", () => {
  let sonsLength = menu.length;

  for (let i = 0; i < sonsLength;) {
    if (menu[i].value != "Select") {
      menu.removeChild(menu[i]);
      sonsLength = sonsLength - 1;
    }
    else
        i++;
  }
})
<select id="menu">
  <option value="Select">Select</option>
  <option value="Madrid">Madrid</option>
  <option value="Barcelona">Barcelona</option>
  <option value="Sevilla">Sevilla</option>
  <option value="Bilbao">Bilbao</option>
  <option value="Manchester">Manchester</option>
  <option value="Paris">Paris</option>
</select>

Answering your question about the else in the for loop:

If you remove an item in an array, all other item indexes move one position forward. E.g.

You have an array

[0, 1, 2]

Now the value 0 is at index 0, the value 1 is at index 1 and the value 2 is at index 2.

When you now remove the first element you get

[1, 2]

So now value 1 is at index 0 and value 2 is at index 1.

For my loop that means, if you remove an element from the array, you must not increment the index (i) because all other elements are moving to the actual index (i). Only if I leave an element I have to increase the index to get to the next element

about 4 years ago · Juan Pablo Isaza Relatório

0

const menu = document.querySelector("#menu");

menu.addEventListener("change", (event) => {
  let selectedValue=event.target.value;
  menu.innerHTML=`<option value=${selectedValue}>${selectedValue}</option>`;
})
<select id="menu">
  <option value="Select">Select</option>
  <option value="Madrid">Madrid</option>
  <option value="Barcelona">Barcelona</option>
  <option value="Sevilla">Sevilla</option>
  <option value="Bilbao">Bilbao</option>
  <option value="Manchester">Manchester</option>
  <option value="Paris">Paris</option>
</select>

Changes

  • I have used the select event
  • used event listner to get the selected value
  • used innerHtml to replace all your option tag with the selected one
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda