Html – Programmatically choose item in select drop down

html

I have a drop down:

<select>  <option value="1">1</option>  <option value="2">2</option></select>

How would I select item 2 programmatically?

Best Solution

First get a handle on that select somehow:

var select = document.getElementsByTagName("SELECT")[0];

Then manipulate the selectedIndex property (I believe it's a zero-based index):

select.selectedIndex = 1;