I am trying to get the value of a .NET dropdown list in JQuery, but I am only able to get the ID of the selected item, not the selected value. Is it possible to do this? This is my current code, which gets the ID of the selected item :
alert($('#<%=ddlReportPeriods.ClientID %>').val());
How can I get the selected value of a drop-down list with jQuery? With jQuery, it’s easy to get selected text from a drop-down list. This is done using the select id. To change the selected value of a drop-down list, use the val () method. You can try to run the following code to learn how to change the selected value of a drop-down list.
This is done using the select id. To change the selected value of a drop-down list, use the val () method. You can try to run the following code to learn how to change the selected value of a drop-down list. Here, we have a default select option first, but the alert shows the second option, which have been changed using the val method ():
Following is the jQuery code that will return the selected value or text from the dropdownlist with validation: alert("Please select valid subject."); As you can see that I’ve bind jQuery built-in change event on document.ready because we need to bind onchange event before using it. Your final code looks like as below:
This index starts from 0 and returns -1 if no option is selected. The options property returns the collection of all the option elements in the <select> dropdown list. The elements are sorted according to the source code of the page. The index found before it can be used with this property to get the selected element.
try
alert($('#<%=ddlReportPeriods.ClientID %> option:selected').text());
You can set an attribute of ASP.NET control which will not change the ID at run time, that is
ClientIDMode = "Static"
Then try,
alert($("#ddlReportPeriods option:selected").val());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With