Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to select a drop down value using plain javascript.?

Let's consider i have a drop down list:
HTML
<select id="drpSelectSourceLibrary" name="asd">
    <option selected="selected" value="All">All</option>
    <option value="ValueOne">Option One</option>
    <option value="ValueTwo">Option Two</option>
    <option value="ValueThree">Option Three</option>
    <option value="ValueFour">Option Four</option>
    <option value="ValueFive">Option Five</option>
    <option value="ValueSix">Option Six</option>
    <option value="ValueSeven">Option Seven</option>
</select>


What I have tried:

We can select the value of drop down with the below javascript code:
javscript
document.getElementById('drpSelectSourceLibrary').value = 'ValueSeven';

But what are the some of the other techniques using which we can select the drop down values using javascript or jquery.?
Posted
Updated 29-May-16 8:21am
v2

1 solution

Here are some of the techniques using which you can select the drop down values.

Method 1:

$('#drpSelectSourceLibrary option')[2].selected = true;

Method 2:

$('#x option[value="5"]').prop('selected', true);

Method 3:

$("#drpSelectSourceLibrary").val('ValueFour');

method 4:
$('select').val("ValueOne");

One can refer this blog Selecting dropdown values in javascript. There are so many techniques to do it.
 
Share this answer
 
Comments
Karthik_Mahalingam 30-May-16 0:31am    
5, for the link.
Animesh Datta 30-May-16 1:00am    
my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900