I have this code to put it in array when user checks the check box.. but when user unchecks the check box how do I need to remove from the Array?
$(function () {
var listval = [];
$('input[name="checkedRecords"]').bind('click', function () {
if ($(this).is(':checked')) {
listval.push($(this).val());
}
else {
//How to remove the Unchecked item from array..
}
});
});
Thanks in advance.
I have this code to put it in array when user checks the check box.. but when user unchecks the check box how do I need to remove from the Array?
$(function () {
var listval = [];
$('input[name="checkedRecords"]').bind('click', function () {
if ($(this).is(':checked')) {
listval.push($(this).val());
}
else {
//How to remove the Unchecked item from array..
}
});
});
Thanks in advance.
Share Improve this question asked Jun 26, 2013 at 15:59 user937194user937194 3833 gold badges6 silver badges12 bronze badges 2- Take a look here: stackoverflow./questions/3596089/… – Irvin Dominin Commented Jun 26, 2013 at 16:01
- 1 Why not generate the array whenever you need it? – Felix Kling Commented Jun 26, 2013 at 16:07
1 Answer
Reset to default 7If you have an array
var mylist = ['a','b','c','d'];
To remove the value 'b':
if ((index = mylist.indexOf('b')) !== -1)
mylist.splice(index, 1);
Results in:
mylist == ['a','c','d'];
For your application:
$(function () {
var listval = [];
$('input[name="checkedRecords"]').bind('click', function () {
if ($(this).is(':checked')) {
listval.push($(this).val());
}
else {
if ((index = listval.indexOf($(this).val())) !== -1) {
listval.splice(index, 1);
}
}
});
});