jQuery: How to check and uncheck a Checkbox

Checking and unchecking, or ticking and unticking if you prefer, checkboxes using jQuery is remarkably simple.

If you think about it, the checked attribute for a standard HTML checkbox is actually a deprecated “checked = true” and by leaving it out in your checkbox declaration, you are in actual fact setting “checked = false” on that input.

So in order to check a checkbox with jQuery you simple select the checkbox input to be affected and enter:

$('input[name=foo]').attr('checked', true);

Alternatively, to uncheck you actually physicall remove the checked attribute:

$('input[name=foo]').removeAttr('checked');

Pretty simple stuff eh? ;)

Related Posts :

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , , , , , . Bookmark the permalink.