$('input[type=button]').on('click', function(){ $('input[type=checkbox]').each(function() { var checked = $(this).prop('checked'); $(this).prop('checked', !checked); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="checkbox" checked /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" checked /> <input type="checkbox" checked /> <input type="button" value="clickme" />
Run the elements. We look at their minted attribute checked
. Take it and change it to the opposite.
UPD. They say each
is optional. So you can even write like this:
$('input[type=button]').on('click', function(){ $('input[type=checkbox]').prop( "checked", function(i, prop) { return !prop; }); });
each
cycle. So that you knew) - Alexey Shimansky