Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting selected value from .NET dropdown using JQuery

Tags:

jquery

asp.net

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());
like image 394
user517406 Avatar asked May 19 '11 13:05

user517406


People also ask

How can I get the selected value of a drop-down list with jQuery?

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.

How to change the selected value of a drop-down list in Salesforce?

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 ():

How to return selected value or text from dropdownlist with validation?

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:

What is options index in dropdown list?

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.


2 Answers

try

alert($('#<%=ddlReportPeriods.ClientID %> option:selected').text());
like image 177
Devjosh Avatar answered Sep 20 '22 16:09

Devjosh


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());
like image 42
Firnas Avatar answered Sep 21 '22 16:09

Firnas