Home > Web Front-end > JS Tutorial > How to Create a 'Select All' Checkbox in HTML?

How to Create a 'Select All' Checkbox in HTML?

DDD
Release: 2024-11-17 14:46:02
Original
213 people have browsed it

How to Create a

How to Select All Checkboxes with a Single "Select All" Checkbox in HTML

Creating an "select all" checkbox in an HTML page allows for the convenient selection of multiple checkboxes with a single click. By implementing this functionality, you can enhance user experience and streamline form submission.

To implement a "select all" checkbox, you can use JavaScript to manipulate the checked property of all other checkboxes:

<script type="text/javascript">
  function toggleAll(source) {
    var checkboxes = document.querySelectorAll('input[type="checkbox"][name="foo"]');
    for (var i = 0; i < checkboxes.length; i++) {
      checkboxes[i].checked = source.checked;
    }
  }
</script>
Copy after login

In this script, the toggleAll() function is defined to toggle the checked state of all checkboxes with the name "foo" whenever the source checkbox (the "select all" checkbox) is clicked.

To use this script, simply add the following checkbox to your HTML:

<input type="checkbox" onclick="toggleAll(this)" /> Select All
Copy after login

Once integrated, clicking the "Select All" checkbox will now toggle the checked state of all other checkboxes on the page with the name "foo." This provides a convenient way for users to quickly and easily select all items in a form.

The above is the detailed content of How to Create a 'Select All' Checkbox in HTML?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template