
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to programmatically set the value of a select box element using JavaScript?
We can set the value of a select box using Javascript using the following. Suppose we have the following select box −
<select id="my-select" value="1"> <option value="1">Select</option> <option value="2">Apple</option> <option value="3">Strawberry</option> <option value="4">Cherry</option> <option value="5">Guava</option> </select>
To set the value of this select element, we need to access it using querySelector. Then set the value. For example −
Example
// Search the select box const mySelectBox = document.querySelector('#my-select'); // Set the value to 3 or Strawberry mySelectBox.value = 3;
- Related Articles
- How to set “value” to input web element using selenium?
- How to set the layout weight of a TextView programmatically in Android using Kotlin?
- How to set the margin of ImageView programmatically in Android using Kotlin ?
- How to swap the key and value of JSON element using JavaScript?
- How to preselect Dropdown Using JavaScript Programmatically?
- How to programmatically set drawableLeft on the Android button using Kotlin?
- How to select different cells of a JTable programmatically in Java?
- How I can set the width and height of a JavaScript alert box?
- How to set the right position of a positioned element with JavaScript?
- How to set the bottom position of a positioned element with JavaScript?
- How to set the stack order of a positioned element in JavaScript?
- How to set the left position of a positioned element with JavaScript?
- Is it possible to manually set the attribute value of a Web Element using Selenium?
- How to set the padding of an element with JavaScript?
- How to set the width of an element in JavaScript?

Advertisements