JQuery Selector checkbox

From CoderLearner
Jump to: navigation, search

Description

Select all elements of type checkbox.

Syntax

$(':checkbox');

Note

For better performance use $(input[type="checkbox"]);

Example 1

It loops through the checkbox and set the checkbox label for each checkbox.

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Select</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>
      <style>
         label input.markit {
            color:red;
            background-color: blue;
         }
      </style>
      <script>
         $().ready(function() {
            $.each($('input:checkbox'),function(){
               var ival=$(this).val();
               var iid = $(this).attr("id");
               $(this).wrap('<label for="'+iid+'">'+ival+'</label>').addClass('markit');
            })
         });
      </script>
   </head>
   <body> 
      <form>
         <input type="checkbox" name="car0" id="car0" value="BMW"/>
         <input type="checkbox" name="car1" id="car1" value="Honda Accord"/>
         <input type="checkbox" name="car2" id="car2" value="Toyota Camery"/>
         <input type="checkbox" name="car3" id="car3" value="Ford Egde"/>
         <input type="text" name="name1"/>
      </form>
   </body>
</html>

Try It