Home > Web Front-end > CSS Tutorial > How to Select All Checkboxes Except One using jQuery?

How to Select All Checkboxes Except One using jQuery?

Susan Sarandon
Release: 2024-11-11 16:07:02
Original
523 people have browsed it

How to Select All Checkboxes Except One using jQuery?

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">
Copy after login

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'));
});
Copy after login

This code does the following:

  1. Gets the checkbox named select_all using its id.
  2. Attaches a change event listener to it.
  3. When the listener is triggered, it finds all checkboxes within the form.
  4. It then sets the checked property of all found checkboxes based on the checked status of the select_all checkbox.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template