[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Get selected text from a drop-down list (select box) using jQuery

  • This topic is empty.
Viewing 15 posts - 16 through 30 (of 31 total)
  • Author
    Posts
  • #9364
    mhandzkie
    Participant

    This work for me:

    $("#city :selected").text();
    

    I’m using jQuery 1.10.2

    #9365
    mohammad-dayyan
    Participant

    the following worked for me:

    $.trim($('#dropdownId option:selected').html())
    
    #9369
    sandeep-shekhawat
    Participant

    Select Text and selected value on dropdown/select change event in jQuery

    $("#yourdropdownid").change(function() {
        console.log($("option:selected", this).text()); //text
        console.log($(this).val()); //value
    })
    
    #9370
    mojtaba
    Participant

    For getting selected value use

    $('#dropDownId').val();
    

    and for getting selected item text use this line:

    $("#dropDownId option:selected").text();
    
    #9361
    bhanu-pratap
    Participant
    $(function () {
      alert('.val() = ' + $('#selectnumber').val() + '  AND  html() = ' + $('#selectnumber option:selected').html() + '  AND .text() = ' + $('#selectnumber option:selected').text());
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
        <title></title>
    
      </head>
      <body>
        <form id="form1" runat="server">
          <div>
            <select id="selectnumber">
              <option value="1">one</option>
              <option value="2">two</option>
              <option value="3">three</option>
              <option value="4">four</option>
            </select>
    
          </div>
        </form>
      </body>
    </html>

    Click to see OutPut Screen

    #9367
    kalaivani-m
    Participant
    var e = document.getElementById("dropDownId");
    var div = e.options[e.selectedIndex].text;
    
    #9359
    max
    Participant

    If you want the result as a list, then use:

    x=[];
    $("#list_id").children(':selected').each(function(){x.push($(this).text());})
    
    #9371
    vishal-thakur
    Participant

    Try:

    $var = jQuery("#dropdownid option:selected").val();
       alert ($var);
    

    Or to get the text of the option, use text():

    $var = jQuery("#dropdownid option:selected").text();
       alert ($var);
    

    More Info:

    #9358
    curtis-yallop
    Participant

    For multi-selects:

    $("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
    
    #9360
    manish-singh
    Participant
    $("#dropdownid option:selected").text();
    

    if you use asp.net and write

    <Asp:dropdownlist id="ddl" runat="Server" />
    

    then you should use

    $('#<%=ddl.Clientid%> option:selected').text();
    
    #9372
    muddasir23
    Participant

    Simply try the following code.

    var text= $('#yourslectbox').find(":selected").text();
    

    it returns the text of the selected option.

    #9357

    Just add the below line

    $(this).prop('selected', true);
    

    replaced .att to .prop it worked for all browsers.

    #9373
    shyamm
    Participant
    $("#dropdown").find(":selected").text();
    
    
    $("#dropdown :selected").text();
    

    $("#dropdown option:selected").text();

    $("#dropdown").children(":selected").text();
    
    #9362
    kamil-kielczewski
    Participant

    Try

    dropdown.selectedOptions[0].text
    
    function read() {
      console.log( dropdown.selectedOptions[0].text );
    }
    <select id="dropdown">
      <option value="1">First</option>
      <option value="2">Second</option>
    </select>
    <button onclick="read()">read</button>
    #9376
    naveenbos
    Participant

    This code worked for me.

    $("#yourdropdownid").children("option").filter(":selected").text();
    
Viewing 15 posts - 16 through 30 (of 31 total)
  • You must be logged in to reply to this topic.