Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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?
$("#yourdropdownid option:selected").text();
ReplyDeleteTry this:
ReplyDelete$("#myselect :selected").text();
For an ASP.NET dropdown you can use the following selector:
$("[id*='MyDropDownId'] :selected")
$("option:selected", $("#TipoRecorde")).text()
ReplyDelete$("#DropDownID").val() will give the selected index value.
ReplyDeleteIf you already have the dropdownlist available in a variable, this is what works for me:
ReplyDelete$("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
If you want a jQuery library to do this and other form tasks, I can impartially* recommend jQuery Extensions.
ReplyDelete* not that impartial, I'm the author
Just in case this helps anyone -- The answers posted above e.g.
ReplyDelete$('#yourdropdownid option:selected').text();
didn't work for me, but this did:
$('#yourdropdownid').find('option:selected').text();
Possibly an older version of jquery?
var someName = "Test";
ReplyDelete$("#<%= 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.
IHTMLElementCollection Options = (IHTMLElementCollection)droplist.getElementsByTagName("option");
ReplyDeletebool Flag = false;
int x = 0;
foreach (HTMLOptionElement Option in Options)
{
x++;
if (Flag)
{
Option.selected = true;
break;
}
if(x==1)
Flag = true;
}