This post will discuss how to bind to HTML checkbox change and click event in JavaScript and jQuery.

There are several ways to bind to HTML checkbox change or click event with JavaScript and jQuery.

1. Using jQuery

With jQuery, you can use the .is() method, which matches the contents of a jQuery object against a selector. The following code demonstrates this with the :checked CSS pseudo-class selector.

JS


HTML


CSS



Edit in JSFiddle

 
Alternatively, you can use jQuery’s .prop() method, which returns true when the input checkbox is checked and false otherwise.

JS


HTML


CSS



Edit in JSFiddle

 
Instead of using jQuery’s is() or prop() method, you can directly access the .checked property of the DOM element to determine whether the checkbox is checked.

JS


HTML


CSS



Edit in JSFiddle

 
Note, all above examples use an attributeEquals selector $('input[type=checkbox][name=gender]') to find all checkboxes. You can also use the :checkbox pseudo-selector $(':checkbox').

Note that you can also bind to the checkbox’s click event instead of the change event.

2. Using JavaScript

With pure JavaScript, you can use the EventTarget.addEventListener() method to bind to the checkbox’s click or change event.

JS


HTML


CSS



Edit in JSFiddle

That’s all about binding to checkbox change and click event in JavaScript and jQuery.