Return a value of the selected item from a dropdown listbox

Discussion in 'Javascript' started by acord, Mar 9, 2006.

  1. acord

    acord Guest

    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
     
    acord, Mar 9, 2006
    #1
    1. Advertising

  2. acord

    Erwin Moller Guest

    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.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
     
    Erwin Moller, Mar 9, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rohith
    Replies:
    3
    Views:
    5,995
    rohith
    Aug 29, 2003
  2. vishnu
    Replies:
    1
    Views:
    1,214
    Patrick.O.Ige
    Mar 25, 2006
  3. akula.sandeepkumar
    Replies:
    0
    Views:
    3,510
    akula.sandeepkumar
    Nov 2, 2006
  4. Eddy Scheire
    Replies:
    6
    Views:
    264
    McKirahan
    Jan 31, 2005
  5. doni
    Replies:
    1
    Views:
    107
Loading...

Share This Page