jQuery & jQuery UI Documentation

jQuery & jQuery UI

: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-1.7rc2.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>

Example:

<!DOCTYPE html>
<html>
<head>
  <style>
input, label { line-height: 1.5em; }
</style>
  <script src="http://code.jquery.com/jquery-1.7rc2.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>