To manipulate the checked or unchecked state of a checkbox using JavaScript, employ the following methods:
// Check: document.getElementById("checkbox").checked = true; // Uncheck: document.getElementById("checkbox").checked = false;
// Check: $("#checkbox").prop("checked", true); // Uncheck: $("#checkbox").prop("checked", false);
// 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!