Return a value of the selected item from a dropdown listbox

A

acord

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.value);
if (objSelect.selected) {
return objSelect.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>&nbsp;
<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
 
E

Erwin Moller

acord said:
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.value);
if (objSelect.selected) {
return objSelect.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>&nbsp;
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top