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.

jQuery and drop down lists

  • 26-10-2007 05:27PM
    #1
    Registered Users, Registered Users 2 Posts: 528 ✭✭✭


    Hi

    I am trying to use JQuery with a drop down list. I cannot get the selected index.

    I know how to register the event for my dd list :

    $("select#myddl").change(function(){


    });


    How do I translate the following code in JQuery :

    How do I go about retreiving the selected value of a dropdown list using jquery?


    <select onchange="f(this.selectedIndex)">
    <option></option>
    <option></option>

    </select>


Comments

  • Posts: 1,882 ✭✭✭ [Deleted User]


    Well jquery objects have a get() method, which returns a DOM element. So you can use properties like selectedIndex. eg. $("myEl").get(0).innerHTML = "Example"; would access the native javascript object of 'myEl' and change it's innerHTML.


  • Registered Users, Registered Users 2 Posts: 528 ✭✭✭Drexl Spivey


    Well jquery objects have a get() method, which returns a DOM element. So you can use properties like selectedIndex. eg. $("myEl").get(0).innerHTML = "Example"; would access the native javascript object of 'myEl' and change it's innerHTML.


    Sounds good. How do you return the index of an option selected using JQuery:

    <select>
    <option value="A">a</option><option value="B">b</option><option
    value="C">c</option>
    </select>


    I want to return 0, 1 and 2 when the above options are selected (in an alert box for ex.)


    How would you go about it?

    I cannot find the answer on jquery.com, google, ....


    Thanks


  • Registered Users, Registered Users 2 Posts: 3,483 ✭✭✭randombar


    Not sure if it's the same thing but I returned javascript commands from an ajax piece I'm using, those javascript commands then populate the select box
    function addNewTreatment( id, name ) {
    document.forms['formname'].elements[ 'treatmentid' ][ document.forms['formname'].elements[ 'treatmentid' ].length ] = new Option( name, id );
    }
    
    


  • Closed Accounts Posts: 1 mariochampion


    hey,

    don't know if you ever found this out.

    var varname =$("select#selectname").val();

    enjoy
    mario


Advertisement