Dilip Raj Baral
Dilip Raj Baral

Reputation: 3070

HTML SELECT - Change selected option by VALUE using JavaScript

There can be many options in a SELECT dropdown.

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

I'm creating a Update Profile page where a user's profile is retrieved from the database and a form is shown with those values. When it comes to SELECTdropdown, there are many options. So, its tedious test all values

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

So, I want to have the corresponding option selected from its value. Like when I do something like sel.value = 'bike' using JavaScript the option 2 should be selected.

Upvotes: 95

Views: 364229

Answers (4)

user5846985
user5846985

Reputation:

document.getElementById('drpSelectSourceLibrary').value = 'Seven';

Upvotes: 0

Michal Klouda
Michal Klouda

Reputation: 14521

You can select the value using javascript:

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

DEMO

Upvotes: 144

Vijay
Vijay

Reputation: 8451

try out this....

JSFIDDEL DEMO

using javascript

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

using jQuery

$('#sel').val('car');

Upvotes: 13

Trevor
Trevor

Reputation: 6689

If you are using jQuery:

$('#sel').val('bike');

Upvotes: 39

Related Questions