How to Check a Radio Button or Checkbox in JavaScript with checked
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
<input>.checked = true;
<input>.checked = false;
<input>.checked
| Part | What it does |
|---|---|
<input> | A checkbox, <input type="checkbox">, or a radio button, <input type="radio">. |
= true | Ticks the checkbox, or selects the radio button. |
= false | Clears it. |
<input>.checked | Reads 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:

<!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 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.checkedreadfalse, because Small isn't selected.- Only one size can be selected. When the script then set
large.checked = true,medium.checkedbecamefalseby itself, because Medium and Large are in the same group.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Setting it to the string 'false' | The box gets ticked: checked became true, because any non-empty string counts as true | Use true and false without quotes |
Expecting a change event | Setting checked in code fired no change event: a listener counted 0 | Call your own code yourself after you set it |
Reading value to see whether it's checked | value is the input's value attribute, even when it isn't checked | Read checked |
Giving radio buttons different names | They're no longer one group, so more than one can be selected | Give every radio button in a group the same name |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Two radio buttons are selected at once | Their names differ | Use the same name |
| The page doesn't react when a script checks a box | Code that listens for change doesn't run for changes made by scripts | Run that code yourself after setting checked |
Cannot set properties of null (setting 'checked') | The input wasn't found | Check 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
- HTMLInputElement, on MDN.
- input type="checkbox", on MDN.
- input type="radio", on MDN.
