How to set the value of a select box element using JavaScript?
In JavaScript, selectedIndex property is used to set the value of a select box element. The selectedIndex property sets or returns the index of the selected value in a drop-down list.
Syntax:
Set index: selectObject.selectedIndex = number
Return index: selectObject.selectedIndex
Note: Integer number is used for Index at the value place.
Example-1: Select a index number.
<!DOCTYPE html> <html> <body> <h1 style="color: green;"> GeeksforGeeks </h1> <select id="mySelect"> <option>football</option> <option>Basketball</option> <option>Hockey</option> <option>Swiming</option> </select> <p> Click the button to select the option element with index "2". </p> <button onclick="myFunction()"> click me </button> <script> function myFunction() { document.getElementById( "mySelect").selectedIndex = "2"; } </script> </body> </html> |
Output:
Before clicking on button, it stays on index 0:

After clicking on button, it go on index 2:

Example-2: If you select selectedIndex = “-1”; it will deselect all the elements of selectbox.
<!DOCTYPE html> <html> <body> <h1 style="color: green;"> GeeksforGeeks </h1> <select id="mySelect"> <option>football</option> <option>Basketball</option> <option>Hockey</option> <option>Swiming</option> </select> <p> Click the button to deselect options. </p> <button onclick="myFunction()"> Click me </button> <script> //Here we delselect all the options function myFunction() { document.getElementById( "mySelect").selectedIndex = "-1"; } </script> </body> </html> |
Output:
Before clicking on button, it stays on index 0:

After clicking on button, all elements gets deselected:

Example-3: If any element is not selected then this property returns -1.
<!DOCTYPE html> <html> <body> <h1 style="color: green;"> GeeksforGeeks </h1> <select id="mySelect"> <option>football</option> <option>Basketball</option> <option>Hockey</option> <option>Swiming</option> </select> <p> Click the button to deselect options. </p> <button onclick="myFunction()"> Click me</button> <script> //Here we delselect all the options function myFunction() { document.getElementById( "mySelect").selectedIndex = "-1"; } //here we are taking value of index function yourFunction() { var x = document.getElementById( "mySelect").selectedIndex; document.getElementById( "demo").innerHTML = x; } </script> <button type="button" onclick="yourFunction()"> Display index </button> <p id="demo"></p> </body> </html> |
Output :

After clicking on button, all elements gets deselected

Here you can see that index return by selectedIndex property is -1

Recommended Posts:
- How to select a random element from array in JavaScript ?
- How to select an element by name with jQuery?
- How to set the default value for an HTML <select> element?
- How to add options to a select element using jQuery?
- How to select all children of an element except the last child using CSS?
- How to select Min/Max dates in an array using JavaScript ?
- How to select first element in the drop-down list using jQuery ?
- How to remove options from select element using jQuery ?
- How to select all text in HTML text input when clicked using JavaScript?
- How to get the type of DOM element using JavaScript?
- JavaScript | Get the text of a span element
- How to get the child element of a parent using JavaScript ?
- Find the min/max element of an Array using JavaScript
- How to get the data attributes of an element using JavaScript ?
- How to check if an element has any children in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



