2

I have an array of divs called "array" that I gathered from the DOM using getElementsByTagName. One of the divs is null (at "nullindex"), and I want to switch its location with the location of one of the other divs (at "index"). I tried:

array[nullindex] = array[index];
array[index] = null;
alert(array[index].innerHTML + "," + array[nullindex].innerHTML);
nullindex = index;

This should switch the locations of the divs and set "nullindex" to its new location, right? But the alert call produces the same innerHTML for both, meaning the div was copied, not switched, and what's supposed to be null is not. So the next time I'm iterating through to find the div I want, it's still at its original position. HELP!!

1
  • note: I think this has something to do with pointers? Commented May 11, 2011 at 23:08

1 Answer 1

2

The problem is the 'array' returned from getElementsByTagName is not an actual array. Before manipulating the elements, run this code:

array = Array.prototype.slice.apply(array);
Sign up to request clarification or add additional context in comments.

2 Comments

I decided to just edit attributes of the divs (id, to be specific) to reflect movement changes, instead of changing the order in the array. Thank you for your help!
since the answer solves the original posted question, make sure you click the accept tick over that-a-way <--

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.