Sometimes you work with forms having a long list of checkboxes, in such scenarios a “Check All” or “Toggle Selection” button comes handy. Such button can save your time by toggling your selection with one click. HTML: `toggleCheckboxes()` function will find all checkboxes inside “fruits” div and check them if not previously checked, and if they are already checked it will uncheck them due to the fact that `!el.checked` is doing negation of their current state.
<div id="fruits"> <input type="button" value="Check/Uncheck All" onclick="toggleCheckboxes();" /> <input type="checkbox" name="color[]" value="Red" id="red"> <label for="red">Red</label> <br /> <input type="checkbox" name="color[]" value="Green" id="green"> <label for="green">Green</label> <br /> <input type="checkbox" name="color[]" value="Blue" id="blue"> <label for="blue">Blue</label> <br /> <input type="checkbox" name="color[]" value="Yellow" id="yellow"> <label for="yellow">Yellow</label> <br /> <input type="checkbox" name="color[]" value="Black" id="black"> <label for="black">Black</label> <br /> </div> Toggle checkboxes with Prototype Js
function toggleCheckboxes(){ $$('#fruits input[type=checkbox]').each(function (el) { el.checked = !el.checked; }); } jQuery function:
function toggleCheckboxes(){ $('#fruits input[type=checkbox]').each(function (el) { $(this).prop('checked',!this.checked); }); }
Toggle checkboxes with jQuery and Prototype JS
Sometimes you work with forms having a long list of checkboxes, in such scenarios a “Check All” or “Toggle Selection” button comes handy
Related posts
JavaScript
nl2br equivalent JavaScript function
Learn how to create an <code>nl2br()</code> equivalent in JavaScript to convert newline characters into HTML line breaks. This guide covers simple JavaScript functions, PHP-to-JavaScript conversion, textarea handling, different newline formats, secure DOM methods, CSS alternatives, and important XSS considerations when working with user-generated content.
JavaScript
Style file upload field
Learn how to transform a standard HTML file upload field into a stylish and user-friendly upload control using jQuery File Style. This guide covers the required jQuery files, implementation steps, customization options, multiple file inputs, and important file upload considerations.
JavaScript
jQuery Alert Dialogs by Bill Beckelman
Learn how jQuery Alert Dialogs by Bill Beckelman can replace basic JavaScript alerts with customizable dialog boxes. This guide covers implementation, form validation, message types, benefits, and key considerations for using jQuery alert dialogs in modern and legacy web applications.