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.

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>
    <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" />

  </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:

Result:

<input type="checkbox" name="newsletter" checked="checked" value="Daily" />,
  <input type="checkbox" name="newsletter" checked="checked" value="Monthly" /> ]

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for :checked Selector below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • Also helpful:

    $("input[name='foo']:checked").length - count of checked,
    $("input[name='foo']").length - $("input[name='foo']:checked").length - count unchecked
  • Or, to count unchecked, you could do this:

    $("input[name='foo']:not(:checked)").length
  • What happened to input:unchecked? That used to work on 1.3.
  • If it worked, it was purely coincidental -- unless you were using a plugin (this one, maybe?). jQuery has never had an :unchecked filter.
  • 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.