Explain Codes LogoExplain Codes Logo

How can I check if a checkbox is checked?

javascript
event-listeners
checkbox-validation
accessibility
Anton ShumikhinbyAnton Shumikhin·Jan 19, 2025
TLDR

For an immediate solution, leverage the .checked property on the checkbox DOM element in JavaScript. So, if you have a checkbox with id="exampleCheckbox", your line of code would be:

var isChecked = document.getElementById('exampleCheckbox').checked;

This check will return a boolean value, true when checked, and false when unchecked.

Checking the checkbox: A journey of one tick

To go beneath just quick solutions, let's delve into the myriad instances and best methodologies on checking a checkbox.

When checkbox changes its mind

One common scenario is, you want to respond to changing checkbox state. Add an event listener and go:

document.getElementById('exampleCheckbox').addEventListener('change', function(e) { if (e.target.checked) { console.log('Just got "Checked" diploma! Celebrate! 🎉'); } else { console.log('Going unchecked, feeling free and wild! 🏄‍♂️'); } });

Proper naming and debugging for dummies

Always pick a name like it's your pet. Clear, unique, and something that gives you joy! Just like this variable called isChecked, ahem, well theoritically any name will be just fine! ⚡

To debug, console.log is your friendly neighborhood Spiderman, catching bugs in the web of your code.

Checkbox validation fashion

Take a trendier path and attach onclick or onchange event directly in your HTML to call a validation function:

<input type="checkbox" id="exampleCheckbox" onclick="validateCheckbox(this)">

Whereas in JavaScript, some JLo fashion trend validation would look like this:

function validateCheckbox(checkbox) { if (checkbox.checked) { // Do the cha-cha when checked } else { // Do the moonwalk when unchecked } }

When jQuery is the cool kid in town!

If you find yourself cruising in jQuery town, then checking a checkbox state is as suave as:

var isChecked = $('#exampleCheckbox').is(':checked');

Just remember, processed food is to health, what jQuery is to performance. Handy but healthy? Nuh-uh. Native JavaScript wins the performance race!

Some things to remember when dealing with checkboxes

A comprehensive checklist when handling all those checkboxes!

Keyboard or Mouse? Be friendly with both!

Inclusivity matters! Mouse or keyboard events, your code must be a friend to all and leave none alone! If change events don't seem to be co-operating, click is your sly friend to turn to!

Accessibility is never overrated

Label your checkboxes well, because even checkboxes like to wear nice labels! Use <label> for usability and accessibility!

<label for="exampleCheckbox">Accept Terms and Conditions</label> <input type="checkbox" id="exampleCheckbox">

Fancy styling for checked checkbox

A little styling never hurts, huh!? The :checked pseudo-class allows you to style your checked checkbox.

input[type="checkbox"]:checked { /* Styling when checked — how about a little party hat? 👒 */ }

Play safe with Security and Edge Cases

Lastly, never trust the clients fully! Always validate checkbox states on the server-side too. Because security is no joke! 🛡️