Context
I am trying to implement such a function that when a user clicks a checkbox in a table, the value and data-title attribute of this checkbox should be stored in the JS object literature named selected as a new element of the key-value pairs array.
If the user clicks the same flag a second time, the corresponding array element must be deleted.
Question
The first time you click the check box, an array is created in the selected object, as expected.
However, when the same flag is clicked a second time, instead of deleting the corresponding array, a new (repeated) one is added .
the code
var selected = {items:[]}; $('#table').on('click', 'input[type="checkbox"]', function() { var found = false; $.each(selected.items, function(i, val) { if (val.key == $(this).attr("value")) { selected.items.splice(i ,1); found = true; return false;
javascript jquery arrays checkbox
Sam32
source share