0

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.

1
  • where is className and item is defined in your code? Commented Jun 29, 2016 at 17:08

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.