I have several input checkbox, for example
5 <input type="checkbox" name="A" class="A">
(with different value)
4 <input type="checkbox" name="B" class="B">
(with different values)
6 <input type="checkbox" name="C" class="C">
(with different values)
and 3 arrays array_A[], array_B[], array_C[] to store input's value every time it's checked or not.
I wrote a function like that to add and remove input's value to the right array
$('input[type="checkbox"]').on('change', function(){
if($(this).is(':checked')){
if($(this).is('.A')){
array_A.push($(this).val());
}else if($(this).is('.B')){
array_B.push($(this).val());
}else if($(this).is('.C')){
array_C.push($(this).val());
}
}else{
if($(this).is('.A')){
position = array_A.indexOf($(this).val());
if ( ~position ) array_A.splice(position, 1);
}else if($(this).is('.B')){
position = array_B.indexOf($(this).val());
if ( ~position ) array_B.splice(position, 1);
}else if($(this).is('.C')){
position = array_C.indexOf($(this).val());
if ( ~position ) array_C.splice(position, 1);
}
}
})
I currently use if else function so many time and I definitely don't think it's a good way to do that, I wonder if is there a better way to do that. Thank you.