Controllare/deselezionare la casella di controllo con JavaScript (jQuery o Vanilla)?

Come si può controllare/deselezionare una casella di controllo usando JavaScript, jQuery o vaniglia?

Soluzione

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);
Commentari (6)

per controllare:

document.getElementById("id-of-checkbox").checked = true;

per deselezionare:

document.getElementById("id-of-checkbox").checked = false;
Commentari (1)

Possiamo controllare una casella di controllo particellare come,

$('id of the checkbox')[0].checked = true

e deselezionare con ,

$('id of the checkbox')[0].checked = false
Commentari (4)