views:

450

answers:

4
<script>
  $(document).ready(function(){

    function countChecked() {
      var n = $("input:checked").length;
      $("div").text(n + (n <= 1 ? " is" : " are") + " checked!");
    }
    countChecked();
    $(":checkbox").click(countChecked);

  });
  </script>
  <style>
  div { color:red; }
  </style>
</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>

i need to get the value of all the checked checkboxes like "Hourly" ,"Daily"

how can i get all the value of the checked checkboxes can anybody help me please

A: 

Not easy to see what you're trying to do here, but I'm guessing this is what you're after:

$("input:checked").each(function() {
   alert($(this).val());
});
peirix
ya , ive already tried that but it doesn't get all of the checked checkboxes value , i need to get all of the checked checkboxes values
jarus
A: 

I think this is what you are after:

function updateChecked(){
 var arr = new Array();
 $('input:checkbox:checked').each( function() {
   arr.push($(this).val());
 });
 var checkboxesText = arr.join(', ');
 var moreText = (arr.length <= 1 ? " is" : " are") + ' checked!';
 $('div').text(checkboxesText + moreText);
}

This would set the div text to something like: "Hourly, Daily are checked!"

Pim Jager
+1  A: 

slight improvement on Pim Jager (guessing you want an array of the values returned):

function getCheckedNames(){
 var arr = new Array();
 var $checked = $('[@name=newsletter]:checked');
 $checked.each(function(){ 
  arr.push($(this).val());
 });
 return arr;
}

and with the improvement from duckyflip:

function getCheckedNames(){
  return jQuery('[@name=newsletter]:checked').map(function(){
   return this.value;
  });
}
Johan
+1  A: 
var values = $.makeArray($(":checkbox:checked").map(function(){
   return this.value 
})).join(', ')

alert(values)
duckyflip