user517406
user517406

Reputation: 13773

getting selected value from .NET dropdown using JQuery

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

Upvotes: 19

Views: 65599

Answers (6)

Ahsan Aftab
Ahsan Aftab

Reputation: 191

<asp:DropDownList ID="ddMainMenuList" runat="server" class="form-control">
                        </asp:DropDownList>

$("[id*=ddMainMenuList]").val("SomeValue");

This is the way you can assign some value to dropdown or textbox or any other asp control. Just you have to mention "id".

Upvotes: 0

Devjosh
Devjosh

Reputation: 6496

try

alert($('#<%=ddlReportPeriods.ClientID %> option:selected').text());

Upvotes: 41

Firnas
Firnas

Reputation: 1695

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

Upvotes: 6

Rory McCrossan
Rory McCrossan

Reputation: 337560

Try this:

alert($('#<%=ddlReportPeriods.ClientID %> OPTION:selected').val());

Upvotes: 1

Pointy
Pointy

Reputation: 413707

If what you want is the textual content of the currently-selected <option> tag under your <select> element, then you could do this:

var $sel = $('#<%=ddlReportPeriods.ClientID %>');
alert($($sel.attr('options')[$sel.attr('selectedIndex')).text());

edit — the other answers with suggestions to use the ":selected" qualifier are better than this ...

Upvotes: 0

gen
gen

Reputation: 405

if it's the same as a regular html dropdown box you can try

$('#<%=ddlReportPeriods.ClientID %> option:selected').val()

Upvotes: 5

Related Questions