Page 1 of 1

Select listbox item using javascript

Posted: Sat Mar 07, 2009 4:06 pm
by Benjamin
I have a bunch of items in a list box. Using an onclick event I want to select one of the items. I'm searching google but not finding anything. I'm using prototype.js.

Code: Select all

 
<script type="text/javascript">
function selectItem() {
    $F('foo').select('1');  // where 1 is the corresponding value
}
</script>
 
<select id="foo" name="bar">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
 
<input onclick="selectItem();" type="button" value="Select Item" />
 

Re: Select listbox item using javascript

Posted: Sat Mar 07, 2009 5:26 pm
by Benjamin
Got it

Code: Select all

 
<script type="text/javascript">
function selectItem() {
  document.getElementById('foo').value = '1';
}
</script>