How do I check or uncheck a checkbox using JavaScript?
In JavaScript, you can check or uncheck a checkbox by accessing the checked property. To check the checkbox:
Checking a Checkbox
- Using the checkbox element’s id
document.getElementById(“checkboxId”).checked = true - Using a DOM element reference
var checkbox = document.querySelector (“input[type=’checkbox’]”
checkbox.checked = true
Unchecking a Checkbox
- Using the checkbox element’s id
document.getElementById(“checkboxId”).checked = false - Using a DOM element reference
var checkbox = document.querySelector (“input[type=’checkbox’]”
checkbox.checked = false
You can also use the click method to toggle the checkbox state:
document.getElementById(“checkboxId”). click
In JavaScript, you can check or uncheck a checkbox by accessing the checked property in your search.