Selecting Checkboxes with jQuery
In web development, JavaScript frameworks like jQuery offer powerful selectors for manipulating DOM elements. One common scenario is selecting multiple checkboxes on a form. Here's how to effortlessly select all checkboxes except for a specific one using jQuery:
Consider a markup like this:
<form> <table> <tr> <td><input type="checkbox">
When the checkbox with id="select_all" is clicked, the goal is to select or deselect all other checkboxes named select[].
Here's a jQuery solution:
$('#select_all').change(function() { var checkboxes = $(this).closest('form').find(':checkbox'); checkboxes.prop('checked', $(this).is(':checked')); });
This code does the following:
This code effectively toggles the checked status of all other checkboxes when the select_all checkbox is clicked. However, it's important to note that the code will only work if the checkboxes are within the same form as the select_all checkbox.
The above is the detailed content of How to Select All Checkboxes Except One using jQuery?. For more information, please follow other related articles on the PHP Chinese website!