jQuery Checkbox Checked - Reading and Setting

| Comments

If you have a checkbox in a form you can use jQuery to read and set the data in the checkbox. Here is the HTML we’ll use an example for our checkbox:

<input type="checkbox" id="checkboxid">

Reading a Checkbox

When using jQuery and you wish to read whether a checkbox is checked or not.


This will return true if the checkbox is checked and false if left unchecked.

Checking or Unchecking the Checkbox

Use the .prop() function to manipulate the checkbox.

$("#checkboxid").prop('checked', true);  // Checks the box
$("#checkboxid").prop('checked', false); // Unchecks the box

The attr() function of jQuery has been deprecated since 1.6. However if you’re jQuery version is below 1.6 you must use this method.

$("#checkboxid").attr('checked', true);

coding, javascript, jquery, webdev