Dilip Raj Baral Dilip Raj Baral - 2 years ago 166
HTML Question

HTML SELECT - Change selected option by VALUE using JavaScript

There can be many options in a SELECT combobox.

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

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
Combo Box, there are many options. So, its hectic and 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.

Answer Source

You can select the value using javascript:

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download