Tuesday, January 21, 2025
HomeProgrammingHow do I check or uncheck a checkbox using JavaScript?

How do I check or uncheck a checkbox using JavaScript?

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
See also  Simple Java Program to say Hello World

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.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x