I know this is old but the question was a bit ambiguous in that toggle may mean each checkbox should toggle its state, whatever that is. If you have 3 checked and 2 unchecked, then toggling would make the first 3 unchecked and the last 2 checked.
For that, none of the solutions here work as they make all the checkboxes have the same state, rather than toggle each checkbox's state. Doing $(':checkbox').prop('checked')
on many checkboxes returns the logical AND between all .checked
binary properties, i.e. if one of them is unchecked, the returned value is false
.
You need to use .each()
if you want to actually toggle each checkbox state rather than make them all equal, e.g.
$(':checkbox').each(function () { this.checked = !this.checked; });
Note that you don't need $(this)
inside the handler as the .checked
property exists in all browsers.