0

I have an ifElseif statement that adds elements to a list using jQuery sortable based on presentationID. I would like to prevent the same element being added more than once.

The idea I cam up with is to create an array called 'disregardList' and before the ifElseif statement use a separate if statement to check if the presentationID is already contained within the array, which it wont be on the first time round. Then execute the ifElseif statement and at the end add the current presentationID to the array. Here's what I have so far.

  function presentationAddToAgenda(presentationId)
           {    

              var disregardList = new Array();

                 if (disregardList[i] != presentationId) 
                    {
                       ifElseif statement.......
                       {
                       Blaa Blaa Blaa 

                       disregardList.push("presentationId");

                    }
            };
1
  • Matt's answer below seems sensible as you're already using jQuery, but newer browsers also have the native Array.indexOf() method.
    – adeneo
    Commented Mar 1, 2013 at 11:29

1 Answer 1

3

You mentioned that you are using jQuery, so you could use the jQuery.inArray() function to test if the value is in your disregard list already, like this:

if ($.inArray(presentationId, disregardList) == -1) { // -1 means not found
    // do stuff
}
4
  • Would it not be necessary to for loop it through the array? Also I'm not sure if I should remove the quotation marks around the presentationID variable when pushing to the array.
    – blarg
    Commented Mar 1, 2013 at 11:32
  • 1
    Quoting something makes it a string, not a variable, so yes, you should remove the quotes, and $.inArray does the looping internally.
    – adeneo
    Commented Mar 1, 2013 at 11:33
  • Hey the code is running ( I can add to the table) but it's still allowing multiples of the same element.
    – blarg
    Commented Mar 1, 2013 at 11:52
  • clearly the push is not functioning as I want. disregardList.push(presentationId); as i have put it before the if statement and elements are still being added.
    – blarg
    Commented Mar 1, 2013 at 12:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.