Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript Drop Down Lists- Check whats selected?

Options
  • 26-04-2009 1:13pm
    #1
    Registered Users Posts: 3,992 ✭✭✭


    Is there any way to check what has been selected with a drop down menu??
    For example?
    <select name="select">
    <option value=1>Answer 1</option>
    <option value=2>Answer 2</option>
    </select>
    

    I know that for a checkbox
    <input type="checkbox" name=answers value=1 />Answer 1
    

    i can use ".checked" in the js function to see if its selected. But what do i use for just a drop down menu?

    Cheers

    EDIT: Forgot at add, the users of this will be selecting their choice and pressing a submit button to send their answer.


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,954 Mod ✭✭✭✭mewso


    Well my preference is to use an id:-
    <select id="myselect">
    <option value=1>Answer 1</option>
    <option value=2>Answer 2</option>
    </select>
    

    so the script is:-
    var select = document.getElementById("myselect");
    var selIndex = select.selectedIndex;
    var opt = select.options[selIndex];
    

    I assume if you are asking you want to do it in javascript but you mention the user will be submitting the form which will often mean you'll want to do the checking on the server which is a different matter.


Advertisement