Home >Web Front-end >JS Tutorial >How to Toggle Checkboxes with JavaScript?

How to Toggle Checkboxes with JavaScript?

Susan Sarandon
Susan SarandonOriginal
2024-11-13 13:26:02507browse

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;
  • jQuery (1.6 ):
// Check:
$("#checkbox").prop("checked", true);

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

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

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!

Statement:
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