0

I'm trying to do some validation on a user selecting items from a list. I want to make sure an item is not added twice by checking if the <li> is already in the array. This is what I'm trying and its not working.

 $(".List").on("click", "li", function () {
 var i = 0;
 var checkArr = [];

 var div = $("#AddedItems");
 var parent = $(this).closest("ul");
 var itemtoadd = parent.find("[data-id]").attr("data-id");
 var name = parent.find("[data-name]").attr("data-name");

 alert(itemtoadd + name);//checking 

 var itemtoadd = ("<li id = " + itemtoadd + " class = \"itemAdd\">" + name + "</li>");

 checkArr.push(itemtoadd); //put one in to check against?
 checkArr.forEach(item)
 {
     if (item == itemtoadd)
         alert("this item has already been added");
     else {
         checkArr.push(itemtoadd);
         alert(itemtoadd);
         $(itemtoadd).appendTo(div);
     }
 }
 // div.html(itemtoadd);


 });
4
  • You can also just use a regular for loop, that's probably faster. Commented Aug 27, 2013 at 19:53
  • I don't know if this is important to your use case but Array.forEach isn't supported in IE 8 and below. Mozilla Developer link Commented Aug 27, 2013 at 20:06
  • I read that. I may move to a .each() Commented Aug 27, 2013 at 20:08
  • Even if you get your for right, you're still going to have two problems, as described in my answer. Handily, you don't actually need a loop here at all. Commented Aug 27, 2013 at 20:21

1 Answer 1

1

You have at least three problems here:

  1. You aren't using Array.forEach correctly -- it takes a function that takes an item.
  2. Immediately before you do your check, you're adding the item you're looking for. You will always hit the alert case.
  3. You're using checkArr as a local variable -- you're getting an empty array each time you enter the function.

That all being said, you can accomplish your goal without keeping an array at all. I believe you can replace everything from your first alert down with this:

if ($('#' + itemtoadd, div).length == 0) {
    itemtoadd = ("<li id = " + itemtoadd + " class = \"itemAdd\">" + name + "</li>");
    div.append(itemtoadd);
}
else {
    alert("this item has already been added");
}
Sign up to request clarification or add additional context in comments.

3 Comments

ok. let me sort this out. My first alert is this? alert(itemtoadd + name);//checking and where does the error message go, in the else?
Also, the user can add up to 4 items so I'm not just checking one item. Every time an item is added I need to check if its already in the div.
Yes, sorry -- I've updated the answer to reflect adding the error message. ... And yes -- I understand that's what you're trying to do. I was just saying that your code as written doesn't do that. Every time an item is added you're creating a new instance of checkArr.

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.