How to pull values from dropdown using jQuery

Discussion in 'jQuery' started by AD Huni, Jan 4, 2014.

  1. #1
    How to pull values from dropdown using jQuery, like top 10, top20, top100, these values are defined in drop down, how to pull these values in a variable and update variable value dynamically based on drop down selection.
     
    AD Huni, Jan 4, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Since "a dropdown" isn't really a semantic container, it's hard to say how the jQuery should look, but if it is, for instance, a list, just do something like:
    
    var listItem = $("#list_name li").text();
    
    Code (markup):
    or something similar

    Or maybe post some code, so we know what we're dealing with...
     
    PoPSiCLe, Jan 4, 2014 IP
  3. rolodex

    rolodex Well-Known Member

    Messages:
    364
    Likes Received:
    37
    Best Answers:
    5
    Trophy Points:
    175
    #3
    Let me help you construct a bare HTML dropdown to start with;
    <select id="countries">
        <option value="US">USA</option>
        <option value="UK">Britain</option>
        <option value="FR">France</option>
    </select>
    HTML:
    And from that structure, you can use jQuery to get anything you want from the selected drop down;
    
    //For the text
    var txt = $('#countries').find('option:selected').text();
    
    //For other attributes inside <option>
    var v = $('#countries').find('option:selected').attr('value');
    
    Code (markup):
     
    rolodex, Jan 7, 2014 IP