Thursday, September 03, 2009

jQuery and checkboxes

Let's start with a simple checkbox like this:

<input type="checkbox" name="foo" value="bar" />

To check whether this checkbox is checked you can do either of the following two alternatives:

$('input[name=foo]').is(':checked')
$('input[name=foo]').attr('checked')

To check the checkbox we can use the attr() function

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

And likewise to uncheck it

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

No comments:


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.