I am having a empty sendStatus[]
. basically i am check the value whether that exist on array while user click on check box. if that's not there i am just add the value with "add+this.value" in case user click on the check box again,
I am checking if the sendStatus[]
contains the "add+this.value" if that's exist i am removing it and adding a new value as "remove+this.value" , so only i will get a value as "add+this.value" or "remove+this.value" in the each of my checkboxes.
to do this i used this function but not works:
var sendStatus = [];
var clearValues = function(newVal){
console.log("calling",newVal);
}
$("form").on("click", ":checkbox", function(e){
var el = $(this),parentClass = $(this).parent().hasClass("green"),label = $(this).parent();
if(!parentClass){
label.addClass("green");
sendStatus = $.grep(sendStatus, function(value) {
return value != "remove" + el.val();
});
if ($.inArray("add" + el.val(), sendStatus) === -1) {
sendStatus.push("add" + el.val());
}
}else{
label.toggleClass("red");
sendStatus = $.grep(sendStatus, function(value) {
return value != "add" + el.val();
});
if ($.inArray("remove" + el.val(), sendStatus) === -1){
sendStatus.push("remove" + el.val());
}
}
console.log(sendStatus);
})
anyone simplify my function and highlight the issue what i am facing.. thanks in advance.