Home > Web Front-end > JS Tutorial > body text

How to Toggle Checkboxes with JavaScript?

Susan Sarandon
Release: 2024-11-13 13:26:02
Original
368 people have browsed it

How to Toggle Checkboxes with JavaScript?

Toggle Checkboxes with JavaScript

To manipulate the checked or unchecked state of a checkbox using JavaScript, employ the following methods:

  • getElementById Method:
// Check:
document.getElementById("checkbox").checked = true;

// Uncheck:
document.getElementById("checkbox").checked = false;
Copy after login
  • jQuery (1.6 ):
// Check:
$("#checkbox").prop("checked", true);

// Uncheck:
$("#checkbox").prop("checked", false);
Copy after login
  • jQuery (1.5-):
// Check:
$("#checkbox").attr("checked", true);

// Uncheck:
$("#checkbox").attr("checked", false);
Copy after login

By utilizing one of these techniques, you can toggle the checked/unchecked state of a checkbox based on user interactions or specific criteria in your web application.

The above is the detailed content of How to Toggle Checkboxes with JavaScript?. 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