This post will discuss how to implement toggle for checkbox using 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 toggle the checked property individually for each checkbox to implement the toggle functionality.

 
This can be done using 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

 
Alternatively, you can simply simulate a mouse-click for all checkboxes using jQuery’s click() method. This is equivalent to calling .trigger('click').

JS


HTML



Edit in JSFiddle

That’s all about implementing toggle for checkbox using JavaScript and jQuery.