Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Mostrar divs según la opción seleccionada dentro del selector con muchas opciones

Tengo un selector #selector con una enorme cantidad de opciones. En diferentes momentos, la cantidad de opciones puede llegar a varias docenas.

Cuando se carga la página, muestro el div #a junto al selector.

Necesito hacer que cuando se seleccionen otras opciones, el resto de los divs estén ocultos y solo se muestre el específico.

Por ejemplo, cuando el usuario selecciona la opción "C" , se debe mostrar un div con el ID #c . Etc.

Utilicé el siguiente script, pero no entiendo por qué no funciona.

Agradecería valiosos consejos.

 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 answers
Answer question

0

Es debido al () que colocó después de .onchange , quítelos y, en su lugar, dígale al programa qué función desea llamar colocándolo allí sin llamarlo.

 document.getElementById("selector").onchange = valueNew;
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!