Sugandika
Sugandika

Reputation: 772

Jquery Issue when getting a checked value of a check box

I have to get a value of a check box using JQuery. When I try out the code below it always returned 'false' as the check box result, even when I check the check box.

var result = $("#checkkBoxId").is(':checked');

Then I tried out the other way, as follows. But it always returned 'undefined' as the result.

var result = $("#checkkBoxId").attr('checked');

Any idea why these code parts are failing to work correctly? Thanks in advance.

Upvotes: 0

Views: 124

Answers (3)

Tim Down
Tim Down

Reputation: 324727

I'd be pretty confident that the problem is that the element your selector matches does not exist or is not a checkbox. Is the double 'k' in your selector a typo?

As an aside, for checking the checkedness of a single checkbox, I strongly urge you to skip jQuery and go directly for the DOM boolean checked property, which could not possibly be simpler to use or more reliable.

SO regular Andy E has a relevant blog post about this kind of thing.

var result = $("#checkkBoxId")[0].checked;

Upvotes: 1

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

It sounds like the ID of the element is wrong. Probably there is a typo: check*k*BoxId in the ID of the checkbox.

Upvotes: 0

Adam
Adam

Reputation: 3665

It looks like more of an issue of do you have the right variable name? User developer tools or firebug to ensure you have the right ID name. If you do not have it set to client mode of static, it gets rendered with a different ID name through the browser.

Share the code of the check box?

Upvotes: 0

Related Questions