I have checkbox with the same name ,I need to get the selected value of checkbox in a array.(It should exclude non selected value). I have following code.
$(document).ready(function() {
$(".t").change(function() {
var allVals = [];
$("input[name='checkbox1']:checked").each(function() {
// alert($(this).val());
allVals.push($(this).val());
});
$('#click').click(function() {
$.each(allVals, function() {
alert(this); // It alerts old past value also
});
});
});
});
<input name="checkbox1" class="t" type="checkbox" value="1"/>a <br/>
<input name="checkbox1" class="t" type="checkbox" value="2"/>b <br/>
<input name="checkbox1" class="t" type="checkbox" value="3"/>c <br/>
<input name="checkbox1" class="t" type="checkbox" value="4"/>d <br/>
<input id="click" id="button" type="button" value="click" />
Here is a JSFiddle with my code -> http://jsfiddle.net/manseuk/VHSrZ/
Can anyone suggest how to solve it ?