Skip to main content

How to Check a Radio Button or Checkbox in JavaScript with checked

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

element.checked = true ticks a checkbox or selects a radio button, and element.checked on its own tells you whether it's ticked or selected right now: true or false. In a group of radio buttons, checking one unchecks the others.

Syntax​

Syntax
<input>.checked = true;
<input>.checked = false;
<input>.checked
PartWhat it does
<input>A checkbox, <input type="checkbox">, or a radio button, <input type="radio">.
= trueTicks the checkbox, or selects the radio button.
= falseClears it.
<input>.checkedReads its current state, true or false.

MDN's HTMLInputElement page lists checked among the input properties: the current state of a checkbox or radio button.

The Example​

The example page has three radio buttons for a size, Small, Medium and Large, and a Gift wrap checkbox, all unchecked in the HTML. The script selects Medium, ticks Gift wrap, and shows whether Small is checked.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a page with three radio buttons Small, Medium and Large in the group size, a Gift wrap checkbox and an empty paragraph, and a script that sets checked to true on medium and gift, then writes whether small is checked into the paragraph.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>checked</title>
</head>
<body>
<p>Size:</p>
<label><input type="radio" name="size" id="small"> Small</label>
<label><input type="radio" name="size" id="medium"> Medium</label>
<label><input type="radio" name="size" id="large"> Large</label>
<p><label><input type="checkbox" id="gift"> Gift wrap</label></p>
<p id="result"></p>

<script>
document.getElementById('medium').checked = true;
document.getElementById('gift').checked = true;
document.getElementById('result').textContent =
'Small is checked: ' + document.getElementById('small').checked;
</script>
</body>
</html>

The three radio buttons share name="size", which makes them one group. Each one has an id, so the script can get it with document.getElementById.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: Size with the radio buttons Small, Medium and Large, Medium selected, a ticked Gift wrap checkbox, and the line Small is checked: false.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Medium is selected and Gift wrap is ticked, set by the script, not by the HTML.
  • small.checked read false, because Small isn't selected.
  • Only one size can be selected. When the script then set large.checked = true, medium.checked became false by itself, because Medium and Large are in the same group.

Common Mistakes​

MistakeWhat happensDo this instead
Setting it to the string 'false'The box gets ticked: checked became true, because any non-empty string counts as trueUse true and false without quotes
Expecting a change eventSetting checked in code fired no change event: a listener counted 0Call your own code yourself after you set it
Reading value to see whether it's checkedvalue is the input's value attribute, even when it isn't checkedRead checked
Giving radio buttons different namesThey're no longer one group, so more than one can be selectedGive every radio button in a group the same name

Troubleshooting​

SymptomLikely causeFix
Two radio buttons are selected at onceTheir names differUse the same name
The page doesn't react when a script checks a boxCode that listens for change doesn't run for changes made by scriptsRun that code yourself after setting checked
Cannot set properties of null (setting 'checked')The input wasn't foundCheck the id, and run the script after the inputs

FAQ​

How do I find which radio button in a group is selected?​

Ask for the checked one: on this page, document.querySelector('input[name=size]:checked') returned the Medium radio button. It returns null when none of them is selected.

How do I start a checkbox ticked without JavaScript?​

Add the checked attribute in the HTML: <input type="checkbox" checked>.

How do I untick every checkbox?​

Loop over them: for (const box of document.querySelectorAll('input[type=checkbox]')) box.checked = false;

Conclusion​

checked is the switch behind every checkbox and radio button: set it to tick or select one, read it to find out whether it is. Setting it doesn't fire change, so run any follow-up code yourself.

Additional Resources​