I'm trying to add some objects to an array if a checkbox is checked. But I can't seem to make it work. What I would like is to add an object depending on the values of the checked checkboxes. I hope you guys can understand it. Let me show you in code :
This is the code to check if a checkbox is checked :
var filter_options=[];
$('input:checkbox').click(function()
{
var name=$(this).val().trim();
if(this.checked)
{
filter_options.push(name);
console.log('Add: ' + name);
}
else
{
var index=filter_options.indexOf(name);
if(index > -1)
{
filter_options.splice(index, 1);
console.log('Remove: ' + name + ' at index: ' + index);
}
}
$('#result').html(filter_options.join('; '));
console.log(filter_options);
});
Let's these are the object:
var human = new Human();
var dog = new Dog();
var cat = new Cat();
var options = []; //push the objects here depending on the checkboxes value.
And this is my html :
<div class="option">
<input type="checkbox" value="human">
<input type="checkbox" value="dog">
<input type="checkbox" value="cat">
</div>
How can I add these objects to the array depending on the checkbox value? Any help would be highly appreciated.