Explore Topics

How Do I Check Whether a Check Box Clicked or Not Using JavaScript?

Last Updated : 18 Apr, 2025 - Asked By Ashok

js  checkbox clicked or not 

Answers
2024-01-01 11:43:32

To check whether a checkbox has been clicked or not in JavaScript, you can use an event listener and check the checked property of the checkbox element. Here's an example:

HTML:
html
<input type="checkbox" id="myCheckbox"> Check Me
<p id="result"></p>


JavaScript:
javascript
// Get the checkbox element
var checkbox = document.getElementById("myCheckbox");

// Add an event listener to the checkbox for the 'change' event
checkbox.addEventListener("change", function() {
  // Check if the checkbox is checked
  if (this.checked) {
    document.getElementById("result").textContent = "Checkbox is checked";
  } else {
    document.getElementById("result").textContent = "Checkbox is unchecked";
  }
});


This code attaches an event listener to the checkbox for the 'change' event. When the checkbox is clicked and its state changes, the event listener triggers a function. Inside this function, it checks the checked property of the checkbox element. If the checkbox is checked, it updates a paragraph (<p>) element with the id "result" to display "Checkbox is checked". If it's unchecked, it displays "Checkbox is unchecked".

You can customize this logic further based on your requirements or perform different actions when the checkbox state changes.

2024-01-01 11:43:43

Using JavaScript to check if a checkbox has been clicked or not:

HTML:
html
<input type="checkbox" id="myCheckbox" onclick="checkBoxClicked()">
<label for="myCheckbox">Check Me</label>
<p id="result"></p>


JavaScript:
javascript
function checkBoxClicked() {
  var checkbox = document.getElementById("myCheckbox");
  var result = document.getElementById("result");

  if (checkbox.checked) {
    result.textContent = "Checkbox is checked";
  } else {
    result.textContent = "Checkbox is unchecked";
  }
}


In this example, the onclick attribute in the HTML checkbox element calls the checkBoxClicked() JavaScript function when the checkbox is clicked. Inside this function, it retrieves the checkbox element and a paragraph element with the id "result". It checks the checked property of the checkbox and updates the paragraph's text content accordingly to display whether the checkbox is checked or unchecked.

This method directly triggers the function when the checkbox is clicked, eliminating the need for an event listener. It provides a simple way to check the checkbox state and update the content dynamically.

Your Answer



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.

People Also Asked