javascript - Store the Div id and checkbox value to an array(multi dimensional) Jquery -


i want store div id , checked value array.

fiddle

suppose check samsung , lenovo brands , 5gb ram, array should be:

array[ ] = ["brand" => "samsung,lenovo" , "ram" => "5gb"] 

html

 <div id="brand"> <input type="checkbox" class="chkbx" value="samsung">samsung<br> <input type="checkbox" class="chkbx" value="nikon">nikon<br> <input type="checkbox" class="chkbx" value="lenovo">lenovo<br> <input type="checkbox" class="chkbx" value="alcatel">alcatel<br>  </div>  <div id="ram"> <input type="checkbox" class="chkbx" value="2 gb">2 gb<br> <input type="checkbox" class="chkbx" value="3 gb">3 gb<br> <input type="checkbox" class="chkbx" value="5 gb">5 gb<br> <input type="checkbox" class="chkbx" value="6 gb">6 gb<br> </div> 

please note divs dynamically grabbed server. illustration, there multiple divs , not possible know id of particular div.

appreciated.
in advance.

$('[type="button"]').click(function() {    var results = [];    $('div').each(function() {      var vals = $(this).find(':checked').map(function() {        return $(this).val();      }).get().join(',');      results[$(this).attr('id')] = vals;    });    console.log(results);  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>  <div id="brand">    <input type="checkbox" class="chkbx" value="samsung">samsung    <br>    <input type="checkbox" class="chkbx" value="nikon">nikon    <br>    <input type="checkbox" class="chkbx" value="lenovo">lenovo    <br>    <input type="checkbox" class="chkbx" value="alcatel">alcatel    <br>  </div>    <div id="ram">    <input type="checkbox" class="chkbx" value="2 gb">2 gb    <br>    <input type="checkbox" class="chkbx" value="3 gb">3 gb    <br>    <input type="checkbox" class="chkbx" value="5 gb">5 gb    <br>    <input type="checkbox" class="chkbx" value="6 gb">6 gb    <br>  </div>  <input type="button" value="get array" />


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -