Reputation: 23
So, I have a bunch of elements that I want the user to be able to select and deselect. However it doesn't seem to be working, I need to store data so I can send them to the server later so I'm doing it by an array. This is my code so far.
Javascript
var items = new Array();
function selectItem(element){
var itemName = element.find(".item").val();
if(jQuery.inArray(className, item)){
element.removeClass("selected");
items.splice($.inArray(itemName, items), 1);
}else{
element.addClass("selected");
items.push(itemName);
}
}
HTML
<div class="Item" onClick="selectItem($(this))">
</div>
It doesn't seem to be doing the if statement and I'm not sure why.
Upvotes: 0
Views: 506
Reputation: 115242
The jQuery jQuery.inArray()
method returns the index of the element if found or -1
in case element not found. So change the if condition as follows
if(jQuery.inArray(className, item) > -1)
Upvotes: 1