Monday, June 4, 2012

jQuery get selected text from dropdownlist


How can I get an asp dropdownlist selected text in Jquery, not using selected value?



Source: Tips4all

9 comments:

  1. $("#yourdropdownid option:selected").text();

    ReplyDelete
  2. Try this:

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


    For an ASP.NET dropdown you can use the following selector:

    $("[id*='MyDropDownId'] :selected")

    ReplyDelete
  3. $("option:selected", $("#TipoRecorde")).text()

    ReplyDelete
  4. $("#DropDownID").val() will give the selected index value.

    ReplyDelete
  5. If you already have the dropdownlist available in a variable, this is what works for me:

    $("option:selected", myVar).text()


    The other answers on this question helped me, but ultimately the following thread helped the most:

    http://forum.jquery.com/topic/this-option-selected-attr-rel-option-selected-is-not-working-in-ie

    ReplyDelete
  6. If you want a jQuery library to do this and other form tasks, I can impartially* recommend jQuery Extensions.

    * not that impartial, I'm the author

    ReplyDelete
  7. Just in case this helps anyone -- The answers posted above e.g.

    $('#yourdropdownid option:selected').text();


    didn't work for me, but this did:

    $('#yourdropdownid').find('option:selected').text();


    Possibly an older version of jquery?

    ReplyDelete
  8. var someName = "Test";

    $("#<%= ddltest.ClientID %>").each(function () {
    $('option', this).each(function () {
    if ($(this).text().toLowerCase() == someName) {
    $(this).attr('selected', 'selected')
    };
    });
    });


    That will help you to get right direction. Above code is fully tested if you need further help let me know.

    ReplyDelete
  9. IHTMLElementCollection Options = (IHTMLElementCollection)droplist.getElementsByTagName("option");
    bool Flag = false;
    int x = 0;
    foreach (HTMLOptionElement Option in Options)
    {
    x++;
    if (Flag)
    {
    Option.selected = true;
    break;
    }
    if(x==1)
    Flag = true;
    }

    ReplyDelete