• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Set Checkbox as Checked in JavaScript

Set Checkbox as Checked in JavaScript

August 10, 2022 Leave a Comment

To set a checkbox as checked using JavaScript, the simplest way is to target the input element and change its checked property to true.

document.getElementById("checkboxInput").checked = true;

If we want to uncheck a checkbox using JavaScript, we can target the input element and change its checked property to false:

document.getElementById("checkboxInput").checked = false;

Let’s say we have the following HTML:

<div id="div">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox 1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-2" value="Checkbox 2" checked>
</div>

To set the first checkbox as checked, we can use the getElementById() method to target its checked property:

document.getElementById("checkbox-1").checked = true;

Note we can also set a checkbox as checked easily using jQuery.

How to Set a Checkbox as Checked on Click with JavaScript

We can check a checkbox using JavaScript very easily by combining the checked property with a click event.

Let’s say we have the following HTML code and we want to set the checked value of our checkbox as checked in the HTML below:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox">
  <label for="checkbox-set-1">Checkbox </label>
  <div id="click-me" onclick="setChecked()">Check Checkbox</div>
</div>

We can utilize an onclick event and getElementById() method to set the checked property of the checkbox.

Below is the JavaScript code which will allow us to set our checkbox as checked.

function setChecked(){
  document.getElementById('checkbox-1').checked = true;
};

The final code and output for this example of how to check a checkbox on click using JavaScript is below:

Code Output:


Check Checkbox

Full Code:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox">
  <label for="checkbox-set-1">Checkbox </label>
  <div id="click-me" onclick="setChecked()">Check Checkbox</div>
</div>

<script>

function setChecked(){
  document.getElementById('checkbox-1').checked = true;
};

</script>

How to Uncheck a Checkbox Using JavaScript

Just as easily as we can check a checkbox, we can also uncheck a checkbox using JavaScript.

Let’s say we have the following HTML code and we want to set the checked value of our checkbox as unchecked in the HTML below:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1-1" value="Checkbox" checked>
  <label for="checkbox-set-1"> Checkbox</label>
  <div class="click-me" onclick="setUnchecked()">Uncheck Checkbox</div>
</div>

We can once again utilize an onclick event and getElementById() method to set the checked property of the checkbox to false.

Below is the JavaScript code which will allow us to set our checkbox as unchecked.

function setUnchecked(){
  document.getElementById('checkbox-1-1').checked = false;
};

The final code and output for this example of how to uncheck a checkbox on click using JavaScript is below:

Code Output:


Uncheck Checkbox

Full Code:

<div id="div1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1-1" value="Checkbox" checked>
  <label for="checkbox-set-1"> Checkbox</label>
  <div class="click-me" onclick="setUnchecked()">Uncheck Checkbox</div>
</div>

<script>

function setUnchecked(){
  document.getElementById('checkbox-1-1').checked = false;
};

</script>

Hopefully this article has been useful for you to understand how to check a checkbox and uncheck a checkbox using JavaScript.

Other Articles You'll Also Like:

  • 1.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 2.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 3.  Push Multiple Items to Array in JavaScript
  • 4.  Using JavaScript to Remove the Last Character From a String
  • 5.  Using JavaScript to Run a Function Every 5 seconds
  • 6.  Examples Using the JavaScript += Addition Assignment Operator
  • 7.  Using JavaScript to Get the Length of Array
  • 8.  Using JavaScript to Get the Domain From URL
  • 9.  Using JavaScript to Get Today’s Date
  • 10.  Reverse For Loop JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy