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

145
Views
Cambiar la opción seleccionada en select con solo javascript

Estoy tratando de cambiar la opción seleccionada de una selección, ya lo he intentado de esta manera:

 document.getElementById("myselect").value = "1"; and also document.getElementById("myselect").selectedIndex = 1;

pero con la primera no funciona, en vez de cambiar a la opción con el valor 1, se va a la última opción del select, y con la segunda no funciona nada. ¿Alguna sugerencia?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar tanto el value como el índice selectedIndex , pero el uso es diferente, consulte el ejemplo a continuación para comprender cómo usarlos.

 const select = document.getElementById("select"); const options = document.querySelectorAll("select option"); // Using selectedIndex document.getElementById("btn").addEventListener("click", () => { const nextIndex = select.selectedIndex + 1; select.selectedIndex = nextIndex % options.length; }); // Using value document.getElementById("orange-btn").addEventListener("click", () => { select.value = "orange"; });
 <select value="apple" id="select"> <option value="apple">Apple</option> <option value="mango">Mango</option> <option value="orange">Orange</option> <option value="kiwi">Kiwi</option> </select> <button id="btn">Next Option</button> <button id="orange-btn">Select Orange</button>

about 4 years ago · Juan Pablo Isaza 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!