jQuery API

:checked Selector

checked selector

version added: 1.0jQuery(':checked')

Description: Matches all elements that are checked.

The :checked selector works for checkboxes and radio buttons. For select elements, use the :selected selector.

Examples:

Example: Finds all input elements that are checked.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<form>
  <p>
    <input type="checkbox" name="newsletter" checked="checked" value="Hourly" />

    <input type="checkbox" name="newsletter" value="Daily" />
    <input type="checkbox" name="newsletter" value="Weekly" />

    <input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
    <input type="checkbox" name="newsletter" value="Yearly" />
  </p>
</form>
<div></div>

<script>
function countChecked() {
  var n = $("input:checked").length;
  $("div").text(n + (n <= 1 ? " is" : " are") + " checked!");
}
countChecked();
$(":checkbox").click(countChecked);
</script>

</body>
</html>

Demo:

Example:

<!DOCTYPE html>
<html>
<head>
  <style>
input, label { line-height: 1.5em; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<form>
  <div>
    <input type="radio" name="fruit" value="orange" id="orange">
    <label for="orange">orange</label>
  </div>
  <div>
    <input type="radio" name="fruit" value="apple" id="apple">
    <label for="apple">apple</label>
  </div>
  <div>
    <input type="radio" name="fruit" value="banana" id="banana">
    <label for="banana">banana</label>
  </div>
  <div id="log"></div>
</form>

<script>
$("input").click(function() {
  $("#log").html( $(":checked").val() + " is checked!" );
});
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :checked Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :checked Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://www.NotifyWire.com Ian Drake

    What happened to input:unchecked? That used to work on 1.3.

  • http://www.learningjquery.com/ Karl Swedberg

    If it worked, it was purely coincidental — unless you were using a plugin (this one, maybe?). jQuery has never had an :unchecked filter.

  • http://www.NotifyWire.com Ian Drake

    Nope, no plugin. Weird. If I had time, I’d switch back to 1.3 and see why it ever worked. Thanks for the reply.

  • http://marienplatz.name/ Anton

    Also helpful:

    $(“input[name='foo']:checked”).length – count of checked,
    $(“input[name='foo']“).length – $(“input[name='foo']:checked”).length – count unchecked

  • http://www.learningjquery.com/ Karl Swedberg

    Or, to count unchecked, you could do this:

    $(“input[name='foo']:not(:checked)”).length

  • http://sites.google.com/site/drizzlesoflearning/ Ramakrishnan t

    for checked count another way
    $(“input[type=checkbox]:checked”).length;

  • http://www.learningjquery.com/ Karl Swedberg

    Sure, that works if you only want to know about checkboxes. Keep in mind, though, that radio buttons can have a “checked” attribute as well. So, $(“input:checked”).length; would count both checkboxes and radio buttons.

  • Anonymous

    How can get the id of the all selected check boxes in form?

  • Anonymous

    //This code will show the alert for all ids that are checked
    (“input:checked”).each(function(){
    alert(this.id);
    });

  • http://blog.robferrer.co.uk Rob Ferrer

    I'm getting an odd situation where
    input[name=foo]:checked
    doesn't work, but
    input[@name=foo]:checked
    does work. Oddly
    input[name=foo]
    still works

    I'm using 1.4.2 from the Google CDN

  • Jeff

    Just solved an annoying problem where I had mistakenly used :CHECKED instead of :checked, and it worked fine in FF 3.6 but broke in IE7/IE8 with an “Object doesn’t support this property or method”. I’m not annoyed that it needs to be lower case, that was my mistake, but if it’s going to break, shouldn’t it break in all browsers?

  • Jeff

    Just solved an annoying problem where I had mistakenly used :CHECKED instead of :checked, and it worked fine in FF 3.6 but broke in IE7/IE8 with an “Object doesn’t support this property or method”. I’m not annoyed that it needs to be lower case, that was my mistake, but if it’s going to break, shouldn’t it break in all browsers?

  • http://www.albertvanhalen.com Albert

    jQuery.extend(jQuery.expr[':'], {
    unchecked: function(element) {
    return ('checkbox' === element.type || 'radio' === element.type) && !element.checked;
    }
    })

  • Tony

    To use the selector with the “this” keyword use :
    $(this).is(':checked')

  • Brent

    I used $(:checked') and in addition to checked checkboxes, it grabbed the selected option from a select.

  • Praveen kuma thalluri

    I used this
    $(“input:checked[name=chkLBrowseItems]“).val()

    I was expecting all the checked values with the comma delimiter, jut the way of posted values. To my frustration it is always providing the first selection value. Is there any way to get my desired behaviour.

  • http://www.learningjquery.com/ Karl Swedberg

    This should do it:
    $(“input:checked[name=chkLBrowseItems]“).map(function() {
    return $(this).val();
    }).get().join();

  • http://twitter.com/tbartels tbartels

    $('input:checkbox').is(':checked').first()

  • http://twitter.com/tbartels tbartels

    As is noted on the documentation for the :not selector the .not() function is better performance:

    $('input[name="foo"]').not(':checked').length

  • Larry

    Thanks for the reply. I am not sure this will work. I need this to check on every check, not matter the order. People are not going to select the checkboxes in order. They may start with #5, and I believe this line of code will work for first selected, correct? I believe I need to loop through the checkboxes like this

    function showCourseLink(){
    // Hide all
    $(“#BoxWrapper span”).hide();
    // Unhide the topmost
    $(“#BoxWrapper”).find('input:checkbox').each(function(){
    var checked = $(this).attr('checked');
    if(checked){
    $(this).parent().next().show();
    // Exit loop
    return false;
    }
    });
    }

  • Larry

    Thanks for the reply. I am not sure this will work. I need this to check on every check, not matter the order. People are not going to select the checkboxes in order. They may start with #5, and I believe this line of code will work for first selected, correct? I believe I need to loop through the checkboxes like this

    function showCourseLink(){
    // Hide all
    $(“#BoxWrapper span”).hide();
    // Unhide the topmost
    $(“#BoxWrapper”).find('input:checkbox').each(function(){
    var checked = $(this).attr('checked');
    if(checked){
    $(this).parent().next().show();
    // Exit loop
    return false;
    }
    });
    }

  • http://twitter.com/ahoereth Alexander H.

    Why does this checkbox not work?
    This is not my code, but i have a similar problem..
    http://schrottplatz.pytalhost.com/temp/jQuerytest.html

  • http://twitter.com/ahoereth Alexander H.

    I still do not know why this problem appears. I couldn’t fix it using the click handler. Now im using change() and it works just fine -.-

  • Hristo D.

    I don't understand why it is done like this. Why not use the default <input type=”checkbox” name=”name” /> only? Then it would work fine and you would still be able to select it and find it through jQuery.

  • http://twitter.com/ahoereth Alexander H.

    It was just an example. In my script I used .click() on the checkbox to trigger a different event – not in direct relation to the checkbox. But it was the same result.

    Using .click() on a checkbox seems to make it uncheckable. even attr(“checked”, “checked”) doesnt work anymore..

  • Ssss

    it is because when you click on checkbox, it is set to checked, then the toggle event of div unchecked it.

    try adding some content to the div, you will see what i mean

  • http://twitter.com/ahoereth Alexander H.

    Ok. Different example.
    http://nopaste.info/0da250ca77.html

    This does not trigger anything in relation to the checkbox, but still the checkbox does not change to checked or the other way around.

    I am now using .change() and it is working fine. Just still wondering why this is..

  • http://pulse.yahoo.com/_LWDBWYN3K2UIP27SAY3OTXHPHM Kevin

    How could one display on screen the value of a checkbox if it is checked?

  • Arun k

    How do I select all the checkboxes that are not checked?

  • Praveen Kumar Thalluri

    $(“input[type=checkbox][checked=false]“)

    Make sure that you got checked property value as false.

  • miao

    $(“input:[type=checkbox][checked=false]“)

    you forgot the colon, not work without!

  • Daniel Xiao

    $(“input:[type=checkbox][checked=false]“)

  • Daniel Xiao

    And In FireFox, how do I select all the checkboxes that are not checked?

  • http://www.learningjquery.com/ Karl Swedberg

    $('input[type=checkbox]:not(:checked)')
    or, preferably,
    $('input[type=checkbox]').not(':checked')

  • http://twitter.com/valeryk Valery Kotlarov

    Hi all,

    var checked = $(this).attr('checked');

    Doesn't work here. Instead
    var checked = !$(this).attr('checked');

    does work. As if right after the click, the attribute is still not updated.
    Any ideas? Thanks…

  • Randy Bo

    jQuery cures all disease and feeds children

  • Randy Bo

    jQuery cures all disease and feeds children