0

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.

user937194
  • 323
  • 3
  • 6
  • 12

1 Answers1

7

If 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);
                }
            }
        });
    });
Joe Frambach
  • 25,568
  • 9
  • 65
  • 95