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

135
Visualizações
Display divs based on selected option inside the selector with many options

I have a #selector selector with a huge number of options. At different times, the number of options can reach several dozen.

When the page loads, I display the #a div next to the selector.

I need to make it so that when other options are selected, the rest of the divs are hidden and only the specific one is displayed.

For example, when the user selects the "C" option, a div with #c ID should be displayed. Etc.

I used the script below but don't understand why it doesn't work.

I would appreciate valuable advice.

function valueNew(ele) {
  var div = document.getElementsByClassName('block');
  // iterating over them and hidding all
  for( var i=0;i<div.length;i++) {
    div[i].style.display = 'none'
  }
  div[ele.value].style.display = 'block';
}
// trigger change event to show default div
document.getElementById('selector').onchange();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="selector">
  <option value="1">A</option>
  <option value="2">B</option>
  <option value="3">C</option>
  <option value="4">D</option>
  <option value="5">E</option>
  <option value="6">F</option>
  <option value="7">G</option>
</select>

<!-- This div is displayed by default on page load. -->
<div id="#a" class="block" style="display: none;">Content for the A option</div>

<div id="#b" class="block" style="display: none;">Content for the B option</div>
<div id="#c" class="block" style="display: none;">Content for the C option</div>
<div id="#d" class="block" style="display: none;">Content for the D option</div>
<div id="#e" class="block" style="display: none;">Content for the E option</div>
<div id="#f" class="block" style="display: none;">Content for the F option</div>
<div id="#g" class="block" style="display: none;">Content for the G option</div>

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's because of the () you placed after the .onchange take them away, and instead tell the program which function you want to call by placing it there without calling it-

document.getElementById("selector").onchange = valueNew;
about 4 years ago · Santiago Trujillo 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