Check/Uncheck checkbox with JavaScript (jQuery or vanilla)?

lisovaccaro picture lisovaccaro · Nov 21, 2011 · Viewed 1.2M times · Source

How can a checkbox be checked/unchecked using JavaScript, jQuery or vanilla?

Answer

Alex Peattie picture Alex Peattie · Nov 21, 2011

Javascript:

// Check
document.getElementById("checkbox").checked = true;

// Uncheck
document.getElementById("checkbox").checked = false;

jQuery (1.6+):

// Check
$("#checkbox").prop("checked", true);

// Uncheck
$("#checkbox").prop("checked", false);

jQuery (1.5-):

// Check
$("#checkbox").attr("checked", true);

// Uncheck
$("#checkbox").attr("checked", false);