Implement “select all” checkbox in HTML with JavaScript/jQuery
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
|
1 2 3 4 5 6 |
document.getElementById('select-all').onclick = function() { var checkboxes = document.getElementsByName('vehicle'); for (var checkbox of checkboxes) { checkbox.checked = this.checked; } } |
HTML
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!doctype html> <html> <body> <p>Choose your vehicle:</p> <div> <input type="checkbox" id="bike" name="vehicle" value="bike"> <label for="bike">Bike</label> </div> <div> <input type="checkbox" id="car" name="vehicle" value="car"> <label for="car">Car</label> </div> <div> <input type="checkbox" id="select-all"> <label for="car">Select All</label> </div> </body> </html> |
⮚ Using Document.querySelectorAll()
JS
|
1 2 3 4 5 6 |
document.getElementById('select-all').onclick = function() { var checkboxes = document.querySelectorAll('input[type="checkbox"]'); for (var checkbox of checkboxes) { checkbox.checked = this.checked; } } |
HTML
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!doctype html> <html> <body> <p>Choose your vehicle:</p> <div> <input type="checkbox" id="bike" name="vehicle" value="bike"> <label for="bike">Bike</label> </div> <div> <input type="checkbox" id="car" name="vehicle" value="car"> <label for="car">Car</label> </div> <div> <input type="checkbox" id="select-all"> <label for="car">Select All</label> </div> </body> </html> |
2. Using jQuery
With jQuery, you can do like:
JS
|
1 2 3 4 5 6 7 8 |
$(document).ready(function() { $('#select-all').click(function() { var checked = this.checked; $('input[type="checkbox"]').each(function() { this.checked = checked; }); }) }); |
HTML
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!doctype html> <html> <body> <p>Choose your vehicle:</p> <div> <input type="checkbox" id="bike" name="vehicle" value="bike"> <label for="bike">Bike</label> </div> <div> <input type="checkbox" id="car" name="vehicle" value="car"> <label for="car">Car</label> </div> <div> <input type="checkbox" id="select-all"> <label for="car">Select All</label> </div> </body> </html> |
A less verbose solution is to use jQuery’s .prop() method.
JS
|
1 2 3 4 5 |
$(document).ready(function() { $('#select-all').click(function() { $('input[type="checkbox"]').prop('checked', this.checked); }) }); |
HTML
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!doctype html> <html> <body> <p>Choose your vehicle:</p> <div> <input type="checkbox" id="bike" name="vehicle" value="bike"> <label for="bike">Bike</label> </div> <div> <input type="checkbox" id="car" name="vehicle" value="car"> <label for="car">Car</label> </div> <div> <input type="checkbox" id="select-all"> <label for="car">Select All</label> </div> </body> </html> |
That’s all about implementing a ‘select all’ check box in HTML in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)