Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Return a value of the selected item from a dropdown listbox

Reply
Thread Tools

Return a value of the selected item from a dropdown listbox

 
 
acord
Guest
Posts: n/a
 
      03-09-2006
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>&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
 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      03-09-2006
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>&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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving the value of an Selected Item from a Listbox widget doni Perl Misc 1 03-14-2007 06:23 PM
Unable to get the selected item value of Listbox in codebehind akula.sandeepkumar ASP .Net 0 11-02-2006 01:04 PM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
Put value of a selected item in Listbox in a text field Eddy Scheire ASP General 6 01-31-2005 03:04 PM
selected item and dropdown list/listbox rohith ASP .Net 3 08-29-2003 04:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57