Form Checked box (input:checked)

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(){<!-- ww  w .ja v a 2  s.co  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

The code above generates the following result.

Form Checked box (input:checked)

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 ww w .ja v  a2  s .co 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

The code above generates the following result.

Form Checked box (input:checked)