I'd like to push checked checkboxes values into an array, but I'm having some issue because each time I try to add it into the array it stays empty, I am probably doing something wrong but can't figure out what.
http://jsfiddle.net/fx7su2rp/689/
HTML :
<input name="checkbox" value="1" type="checkbox" />
<input name="checkbox" value="2" type="checkbox" />
<input name="checkbox" value="3" type="checkbox" />
<input name="checkbox" value="4" type="checkbox" />
<button id="button" type="button">button</button>
Jquery :
$(document).ready(function () {
var tmp = [];
$("input").each(function() {
if ($(this).is(':checked')) {
var checked = ($(this).val());
tmp.push(checked);
}
});
$('#button').on('click', function () {
alert(tmp);
});
});
What am I doing wrong ?