Checking checkbox state in Javascript and JQuery

Posted on 24-01-2013 17:50 by graham
In this tutorial we will discuss various ways of checking whether a checkbox is checked, using both native Javascript and JQuery.

Native Javascript


To check if a checkbox is checked in plain Javascript, use the (surprise!) checked attribute on the input object.
if (document.getElementById("checkboxId").checked == true)
{
alert("Checkbox is checked");
}
else
{
alert("Checkbox is unchecked");
}

See how it works or try to break it in our code sandbox.

JQuery


The same check in JQuery looks like this:
if ($("#checkboxId").is(":checked"))
{
alert("Checkbox is checked");
}
else
{
alert("Checkbox is unchecked");
}
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems