acord wrote:
> Hi,
>
> I am having problem to get a value of the selected item from a dropdown
> listbox.
>
> Here is the JS function;
> function getSelectedItem(objSelect) {
> alert("in getSelectedItem");
> alert (objSelect.length);
> alert (objSelect.value);
> for (i=0; i<objSelect.length; i++) {
> alert("recahed here.."+i)
> alert (objSelect[i].value);
> if (objSelect[i].selected) {
> return objSelect[i].value;
> }
> }
> return "";
> }
>
> here is corresponding html:
>
> <form name="main_form">
> <table>
> <tr><td>
> <select name="assigned_num">
> <option value='null'>-- Select Number --</option>
> <option>1</option>
> <option>2</option>
> <option>3</option>
> <option>4</option>
> <option>5</option>
> </select>
> <input type="button" class="button" value="Add"
> onclick="javascript:getSelectedItem('assigned_num:
> '+document.main_form.assigned_num.value);">
> </td></tr>
>
> </table>
> </form>
>
> What s wrong with this code and what s the correct implementation for
> retrieving a selected value from the listbox( or dropdown listbox)?
>
> Thanks
> A
Hi,
To get the selected one use selectedIndex property of the select-object.
Thus:
<form name="fake">
<select name="theSelector" onChange="alertIt();">
<option value="apple">apples
<option value="pear">pears
<option value="donut">donuts
</select>
</form>
<script type="text/javascript">
function alertIt(){
var TheSelectedIndex = document.forms.fake.theSelector.selectedIndex;
var TheSelectedValue =
document.forms.fake.theSelector[TheSelectedIndex].value;
var TheSelectedText =
document.forms.fake.theSelector[TheSelectedIndex].text;
alert ("index = "+TheSelectedIndex );
alert ("corresponding option-value = TheSelectedValue );
alert ("corresponding option-text= "+TheSelectedText );
}
</script>
Good luck.
Regards,
Erwin Moller
|