Mike K
Mike K

Reputation: 1313

Cannot get the checkbox.checked property

I have some ASP.Net code that defines a server-side checkbox like this:

<asp:CheckBox ID="_chkWindAndHail" runat="server" Text="Wind / Hail Deductible" TextAlign="Right" Checked="true" onClick="onControlChanged()" />

Then in the client-side event handler I have some Javascript that looks like this:

var chkWind = $("#<%= _chkWindAndHail.ClientID %>");
var hdnWind = $("#<%= _hdnWindAndHailPremium.ClientID %>");
var txtWind = $("#<%= _txtWindAndHailPremium.ClientID %>");
if (chkWind.checked)
    txtWind.val(hdnWind.val);
else
    txtWind.val(0);

When I try to look at the value of the "checked" property of the textbox, Firebug says the checkbox object is good but the "checked" property comes up "undefined" and thus in code it never sets a proper value.

Can someone explain to me what the heck I'm doing wrong? I'm new to JQuery, so this is probably something very basic.

Upvotes: 0

Views: 1775

Answers (3)

user113716
user113716

Reputation: 322592

Fastest way is to get the checked property, but you need to do it from the DOM element itself stored in the jQuery object.

chkWind[0].checked;

This retrieves the DOM element at index 0, and gives you the checked property. You were trying to get it from the jQuery object instead of the element.

Upvotes: 1

Loktar
Loktar

Reputation: 35319

Try

.is(':checked'); 

or

.attr('checked'); 

Both should bring back the result you are looking for.

Upvotes: 3

Thomas
Thomas

Reputation: 2162

Try chkWind.attr('checked') to access the attribute. Values returned as true/false.

Upvotes: 0

Related Questions