javascript - Getting multiple checked checkbox labels into an array -


i hoping checked checkboxes form array.

here did.

$("div.category-panel input:checked").next ('label').text(); gets checked checkboxes shows them 1 text. example, checkbox1, checkbox2 , checkbox3 (if checked) show checkbox1checkbox2checkbox3.

i hoping these different checkboxes in array can use them.

    $('.submit').on("click", function(e) {      //got checked checkboxes 'children'.                 children = $("div.category-panel input:checked").next ('label').text();      //put in array.                 var array = [];     var = 0;     $.each(children, function(key, value){         array.push($(this).text());     });      //show array.     (var = 0; < array.length; i++) {       console.log(array[i]);     }   }); 

html, in case is:-

<div class="category-panel">  <input type="checkbox" name="name_service2" id="tid-46" value="46" checked="checked"> <label class="option" for="edit-tid-46">checkbox1</label>  <input type="checkbox" name="name_service3" id="tid-47" value="47" checked="checked"> <label class="option" for="edit-tid-47">checkbox2</label>  <input type="checkbox" name="name_service4" id="tid-44" value="44" checked="checked"> <label class="option" for="edit-tid-44">checkbox3</label>  <input type="checkbox" name="name_service5" id="tid-48" value="48" checked="checked"> <label class="option" for="edit-tid-48">checkbox4</label>  </div> 

you can use .map() like

 $('.submit').on("click", function (e) {       //got checked checkboxes 'children'.                  var array = $("div.category-panel input:checked").next('label').map(function(){          return $(this).text();      }).get();       //show array.      (var = 0; < array.length; i++) {          console.log(array[i]);      }   }); 

$('.submit').on("click", function(e) {       //got checked checkboxes 'children'.                 var array = $("div.category-panel input:checked").next('label').map(function() {       return $(this).text();     }).get();       //show array.     (var = 0; < array.length; i++) {       console.log(array[i]);     }     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div class="category-panel">    <input type="checkbox" name="name_service2" id="tid-46" value="46" checked="checked" />    <label class="option" for="edit-tid-46">checkbox1</label>    <input type="checkbox" name="name_service3" id="tid-47" value="47" checked="checked" />    <label class="option" for="edit-tid-47">checkbox2</label>    <input type="checkbox" name="name_service4" id="tid-44" value="44" checked="checked" />    <label class="option" for="edit-tid-44">checkbox3</label>    <input type="checkbox" name="name_service5" id="tid-48" value="48" checked="checked" />    <label class="option" for="edit-tid-48">checkbox4</label>  </div>  <button class="submit">submit</button>

in case children string, contains concatenated text of label's next siblings of checked checkboxes


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -