

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to preselect a <select> list item using JavaScript?
To preselect a <select> list item using JavaScript, use the selectedIndex property. Add an index of what you want to be selected for this property.
Here, under the <script> tag, add_select_id is the id of the <select> tag, whereas add_item_index is the index in numbers. This index is the list item index, you need to add for the item you want to be preselected.
Example
You can try to run the following code to preselect a list item using JavaScript −
<!DOCTYPE html> <html> <head> <title>Preselect List Item</title> </head> <body> <p> Select any one:</p> <form> <select id="dropdown" name="dropdown" class="form-control"> <option value = "Java">Java</option> <option value = "Discrete Mathematics">Discrete Mathematics</option> <option value = "Digital Electronics">Digital Electronics</option> </select> </form> <script> document.getElementById("dropdown").selectedIndex = "2"; </script> </body> </html>
- Related Questions & Answers
- How to preselect Dropdown Using JavaScript Programmatically?
- How to select an item from a dropdown list using Selenium WebDriver with java?
- How to randomly select an item from a list in Python?
- How to preselect a value in a dropdown list of items in HTML forms?
- How to add a list item in HTML?
- How to set the list-item marker type with JavaScript?
- How to insert an item into a C# list by using an index?
- How to remove an item from a C# list by using an index?
- How to select multiple options in a dropdown list with JavaScript?
- How to randomly select an item from a tuple in Python?
- How to randomly select an item from a string in Python?
- How to display a value when select a JList item in Java?
- How to add an item to a list in Kotlin?
- Disable a list item in a Bootstrap list group
- How to set an image as the list-item marker with JavaScript?
Advertisements