jQuery and ASP.NET

September 22, 2009

Retrieve the Value of an ASP.NET CheckBox in jQuery




In this post, let us see how to retrieve the text of an ASP.NET CheckBox using jQuery

Declare an ASP.NET CheckBox control as shown below:

<asp:CheckBox ID="cb1" runat="server" Text="Option One" />

Now a lot of users try to retrieve the text of this checkbox in the following manner:

alert($("#Checkbox1").val());

However this code does not work for an ASP.NET Checkbox control. Let us see why. This control renders to the following HTML

<input id="Checkbox2" type="checkbox" name="cb1" />
<
label for="cb1">Option One</label>

If you observe, a label control gets created which holds the text for the CheckBox.

In order to retrieve the text of this checkbox, change the jQuery code to:

alert($("input:checkbox[id$=cb1]").next().text());

and now the code would alert ‘Option One’

Note: The same code could have also been written as alert($("#cb1").next().text()). However the code given above works in the case of a MasterPage too. I prefer the above one!



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

0 Responses to "Retrieve the Value of an ASP.NET CheckBox in jQuery"
 

Copyright © 2009-2011 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions