HTML SELECT - change the selected option using VALUE using JavaScript

There can be many parameters in the SELECT drop-down list.

<select id="sel"> <option value="car">1</option> <option value="bike">2</option> <option value="cycle">3</option> ... </select> 

I am creating an update profile page in which a user profile is retrieved from the database and the form is displayed with these values. When it comes to the SELECT dropdown, there are many options. So, his tedious test for all values

 if (value == '1') echo "<option selected value='car'>1</option>";` 

So, I want the appropriate option to be selected from its value. For example, when I do something like sel.value = 'bike' using JavaScript, select option 2 .

+65
javascript html html-select
Sep 04
source share
4 answers

You can select a value using JavaScript:

 document.getElementById('sel').value = 'bike'; 

Demo

+99
Sep 04
source share

If you are using jQuery:

 $('#sel').val('bike'); 
+37
Sep 04
source share

try it....

JSFIDDEL DEMO

using javascript

 ​document.getElementById('sel').value = 'car';​​​​​​​​​​ 

using jQuery

 $('#sel').val('car'); 
+9
Oct 12 '13 at 10:22
source share
 document.getElementById('drpSelectSourceLibrary').value = 'Seven'; 
+1
May 25 '16 at 19:13
source share



All Articles