Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript Drop Down Lists- Check whats selected?

  • 26-04-2009 12:13PM
    #1
    Registered Users, Registered Users 2 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: 9,221 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