You are updating and pushing the same array reference inside the callback function. Instead use a local variable inside callback or generate array directly and push to the aData array.
Although you can refer the element using this inside the callback so there is no need to use callback arguments.
var aData = [];
$(".providers_chk").each(function () {
if (this.checked) { // check the checked property
aData.push([this.value, this.name]);
}
});
$('.providers_chk').change(function() {
var aData = [];
$(".providers_chk").each(function() {
if (this.checked) {
aData.push([this.value, this.name]);
}
});
console.log(aData);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="abc" class="providers_chk" value="1" />
<input type="checkbox" name="abc" class="providers_chk" value="2" />
<input type="checkbox" name="abc" class="providers_chk" value="3" />
<input type="checkbox" name="abc" class="providers_chk" value="4" />
<input type="checkbox" name="abc" class="providers_chk" value="5" />
<input type="checkbox" name="abc" class="providers_chk" value="6" />
<input type="checkbox" name="abc" class="providers_chk" value="7" />
if:checkedvar aData = [];
$(".providers_chk:checked").each(function () {
aData.push([this.value, this.name]);
});
$('.providers_chk').change(function() {
var aData = [];
$(".providers_chk:checked").each(function() {
aData.push([this.value, this.name]);
});
console.log(aData);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="abc" class="providers_chk" value="1" />
<input type="checkbox" name="abc" class="providers_chk" value="2" />
<input type="checkbox" name="abc" class="providers_chk" value="3" />
<input type="checkbox" name="abc" class="providers_chk" value="4" />
<input type="checkbox" name="abc" class="providers_chk" value="5" />
<input type="checkbox" name="abc" class="providers_chk" value="6" />
<input type="checkbox" name="abc" class="providers_chk" value="7" />
Or you can use jQuery.map method and :checked pseudo-class selector to make it simpler.
var aData = $.map($(".providers_chk:checked"), function(e) {
return [e.value, e.name];
});
$('.providers_chk').change(function() {
var aData = $.map($(".providers_chk:checked"), function(e) {
return [e.value, e.name];
});
console.log(aData);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="abc" class="providers_chk" value="1" />
<input type="checkbox" name="abc" class="providers_chk" value="2" />
<input type="checkbox" name="abc" class="providers_chk" value="3" />
<input type="checkbox" name="abc" class="providers_chk" value="4" />
<input type="checkbox" name="abc" class="providers_chk" value="5" />
<input type="checkbox" name="abc" class="providers_chk" value="6" />
<input type="checkbox" name="abc" class="providers_chk" value="7" />
{"foo","bar"}is not a valid syntax. - Nina Scholzif(), you can use the selector.providers_chk:checked. - Barmar