Get checkbox list values with jQuery
$(document).ready(function() { $('#someButton').click(function() { var names = []; $('#MyDiv input:checked').each(function() { names.push(this.name); }); // now names contains all of the names of checked checkboxes // do something with it });});
Since nobody has mentioned this..
If all you want is an array of values, an easier alternative would be to use the .map()
method. Just remember to call .get()
to convert the jQuery object to an array:
var names = $('.parent input:checked').map(function () { return this.name;}).get();console.log(names);
Pure JavaScript:
var elements = document.querySelectorAll('.parent input:checked');var names = Array.prototype.map.call(elements, function(el, i) { return el.name;});console.log(names);
var aArray = [];window.$( "#myDiv" ).find( "input[type=checkbox][checked]" ).each( function(){ aArray.push( this.name );});
You can put it in a function and execute on click of the button.