1

That is what i'm trying to do:

I have a list of divs with the same class, everytime I click on one of the divs I would like to store the ID of the respective div in an array, something like:

var array = [];

[CODE]

$("divID").on("click", function(){

  array.push($(this).attr("id"));

[CODE]

});

What I want to accomplish is that any other click on the same div push/splice the matching ID within the array. A push/splice toggle by ID I guess...

Pushing the div IDs inside an array is not the problem however removing them seems to be a bit more problematic since I'm only capable of clearing the complete array.

How would I have to loop through the array in order to find the matching ID and toggle it on click?

Thanks!

2
  • 1
    A simpler approach would be to add {ID: 'on'} when clicking, then update the ID and set to 'off' when clicking again. This way, you don't have to remove any items from the array--simply update its status. Commented May 31, 2016 at 16:33
  • Thank you for the quick answer! That approach works! Commented May 31, 2016 at 21:40

2 Answers 2

1

As stated in my OP comment, a simpler approach would be to use a Hash instead of a simple array. Doing this, you can add {ID: 'on'} when clicking, then update the ID and set to 'off' when clicking again. This way, you don't have to remove any items from the array--simply update its status.

For example:

var hash = {};

$("divID").on("click", function(){
  var id = $(this).attr("id");

  // Turns it on
  hash[id] = 'on';               // 'on' or true

  // Alternatively, turn it off
  hash[id] = 'off';              // 'off' or false

  // You can also check its status
  // if (hash[id] == 'on')...
});
Sign up to request clarification or add additional context in comments.

Comments

0

UPDATE: concerning the original approach:

I found the answer to the original approach of adding/removing the values of the array here (Reference):

http://www.thoughtdelimited.org/thoughts/post.cfm/jquery-tip-finding-adding-and-removing-values-from-arrays#respond

And this is the code:

var index= jQuery.inArray(theValue, theArray);

//If you're not using any other Javascript library that utilizes the $ sign, you can use the $ sign 
//instead of the jQuery namespace

var index= $.inArray(theValue, theArray);
if(index== -1)
   {
     //Value not found in the array.  Add to the end of the array with push();
     theArray.push(theValue);
   }
else
   {

     //Remove from the array using splice(). Splice takes the index value of where you want to start 
     //removing items from the array as the first parameter, and then the number of item you want 
     //to remove as the second parameter.

     theArray.splice(index,1);

   }

Worked also out for me, I hope it will help others.

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.