How to check if a checkbox is checked using jQuery and set/unset it programmatically

Let’s say you have a checkbox like the following

<input type=”checkbox” id=”leCheckBox”>

To check if it’s checked or not, use:

var leCheckBoxIsChecked = $(“#leCheckBox:checked”).length>0;

It will returns true if the checkbox is checked and false otherwise.

To check the checkbox programmatically:

$(“#leCheckBox”).prop(“checked”, “true”);

To uncheck the checkbox programmatically:

$(“#leCheckBox”).removeAttr(“checked”)

Leave a Reply

Your email address will not be published. Required fields are marked *