This post will discuss how to implement a “select all” checkbox in HTML in JavaScript and jQuery.

1. Using JavaScript

With pure JavaScript, you can use the checkbox’s checked property to set the checked state of a checkbox. However, you need to set the checked property individually for each checkbox.

 
To get the list of checkboxes, you can use the getElementsByName() or querySelectorAll() method.

⮚ Using Document.getElementsByName()

JS


HTML



Edit in JSFiddle

⮚ Using Document.querySelectorAll()

JS


HTML



Edit in JSFiddle

2. Using jQuery

With jQuery, you can do like:

JS


HTML



Edit in JSFiddle

 
A less verbose solution is to use jQuery’s .prop() method.

JS


HTML



Edit in JSFiddle

That’s all about implementing a ‘select all’ check box in HTML in JavaScript and jQuery.