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); }); }
JavaScript
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
· 5 min read
Related posts
JavaScript
JQuery Multi fileupload
File Upload widget with multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumabl
JavaScript
JQuery tooltip
Here is a nice jQuery tool tip. How to use: 1- Download the file 1.1- Rename the file tipsy-zip.doc to tipsy.zip 1.2- Extract the zip folder or download it from
JavaScript
Get selected value of radioButtonList using jQuery in Yii
Sometimes you want to get selected value of radioButtonList using jQuery in your Yii powered application and it feels difficult due to Yii’s field naming