faqts : Computers : Programming : Languages : JavaScript : Forms : SELECT

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

34 of 53 people (64%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do I get the value from a selected option in a dynamically generated listbox?

Oct 3rd, 2003 15:25
Jack Coates, Arsen Yeremin, Bob Stirling,


<html>
<head>
	<title>My Listbox</title>
  <script>
    function WhatIsSelected()
    {  var obj_list_box = document.my_form.my_select;
       alert(obj_list_box.options[obj_list_box.selectedIndex].value)
    }
  </script>
</head>
<body>
<form name="my_form">
  <select name="my_select">
    <option value="a">a</option>
    <option value="b">b</option>    
    <option value="c">c</option>    
    <option value="d">d</option>
    <option value="e">e</option>        
  </select>
</form>
<a href="javascript:WhatIsSelected()">What is Selected</a>
</body>
</html>
that's not actually a dynamically generated menu, unfortunately. This is:
<html>
<head>
	<title>My Listbox</title>
  <script>
    var source = new Array("('Please Select...'),'',true,true)",
    "('1')","('2')","('3')");
    var mydoc = document.my_form.my_select
    while (source.length < mydoc.length) {
        mydoc.options[(mydoc.options.length -1)] = null;
    }
    for (var i=0; i < source.length; i++) {
        eval("mydoc.options[i]=" + "new Option" + source[i]);
    }
    function WhatIsSelected()
    {  var obj_list_box = document.my_form.my_select;
       alert(obj_list_box.options[obj_list_box.selectedIndex].value)
    }
  </script>
</head>
<body>
<form name="my_form">
  <select name="my_select">
  </select>
</form>
<a href="javascript:WhatIsSelected()">What is Selected</a>
</body>
</html>
Can anyone force selection of an item in it?