jQuery Form Checked box selector

Description and Syntax

$(":checked")

selects all form elements-checkboxes and radio buttons-that are checked.

Examples

The following code does the count for checked form elements.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from w w w.j  a  v a2s .  c  o  m-->
            alert($("input:checked").length);
        });
    </script>
  </head>
  <body>
    <body>
    <form>
        <input type="button" value="Input Button"/>
    
        <input type="checkbox">java2s.com</input>
        <input type="checkbox" />
        <input type="checkbox" />
        <input type="file" />
        <input type="hidden" />
        <input type="image" />
        <input type="password" />
        <input type="radio" />
        <input type="reset" />
        <input type="submit" />
        <input type="text" />
    
        <select><option>Option<option/></select>
        <textarea></textarea>
        <button>Button</button>
    </form>    
    </body>
</html>

Click to view the demo

Checked sibling

The following code finds all inputs that are not checked and highlights the next sibling span.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from  w ww  .  j  a va  2s .c  o  m-->
           $("input:not(:checked) + span").css("background-color", "yellow");
        });
    </script>
  </head>
  <body>
    <body>
    <div>
        <input type="checkbox" name="a" />
        <span>A</span>
      </div>
      <div>
        <input type="checkbox" name="b" />
        <span>B</span>
      </div>
      <div>
        <input type="checkbox" name="c" checked="checked" />
        <span>C</span>
    </div>
    </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities