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

141
Views
Change the selected option in select with only javascript

I'm trying to change the selected option of a select, I have already tried this way:

document.getElementById("myselect").value = "1";

and also 

document.getElementById("myselect").selectedIndex = 1;

but with the first one, it doesn't work, instead of changing to the option with the value 1, it goes to the last option of the select, and with the second one it doesn't work at all. Any suggestions?

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

0

You can use both value and selectedIndex but the usage is different, check the example below to understand how to use them.

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!