Tutorial :get value of checked[ALL] or unchecked box jquery



Question:

I have read this.

Jquery checkbox

<input type="checkbox" name="checkGroup" id="all">  <input type="checkbox" name="checkGroup" id="one" value="1">  <input type="checkbox" name="checkGroup" id="two" value="2">  <input type="checkbox" name="checkGroup" id="three" value="3">  <input type="hidden" name="storeCheck" value="">      $(function(){      $("#all").click(function(){          $("input:checkbox[name='checkGroup']").attr("checked",$(this).attr("checked"));           //array[1,2,3] will be pass to value of storeCheck      });        $("input:checkbox[name='checkGroup']:not('#all')").click ( function(){          var totalCheckboxes = $("input:checkbox[name='checkGroup']:not('#all')").length;          var checkedCheckboxes = $("input:checkbox[name='checkGroup']:not('#all'):checked").length;            if ( totalCheckboxes === checkedCheckboxes )          {              $("#all").attr("checked" , true );          }          else          {              $("#all").attr("checked" , false );          }      });  });  

Demo

I am trying to get the value of the checkboxs are checked as an array.

for example

if I checked All       Get value  array_check = 1,2,3 and  passed this array to hidden name="storeCheck"  otherwise:      Get value of array_check( checkboxs checked ).and  passed this array to hidden name="storeCheck"  


Solution:1

It's not clear wether you want to do it client-side or server-side. Anyway that's a JQuery snippet that fills an array with all the checked checkBoxes:

var checkedCheckboxes = new Array();  $('input:checkbox[name="checkGroup"]').each(function(index) {    if ($(this).attr('checked') == 'true') { checkedCeckboxes.push(this);}  });  


Solution:2

 var checkedCheckboxes = $(":checkbox:checked").toArray();  


Solution:3

Try:

function getChecked ()  {      var array;      var addition;      var count = 0;      for (var k = 1; k < document.getElementById('boxes').elements.length; k++)      {           if (document.getElementById('boxes').elements[k].checked == true)           {               addition = k + '';               array = array + '_' + addition;               count = count + 1;           }      }  }  

And using the gollowing html code:

<form id='boxes' name='boxes'>      <input name='list' type='checkbox' id='checkbox' value='1'>      <input name='list' type='checkbox' id='checkbox' value='2'>      <input name='list' type='checkbox' id='checkbox' value='3'>      <input name='list' type='checkbox' id='checkbox' value='4'>  </form>  

And so on...


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »