0

Assuming that cache["s"].length = 9, and cache["n"] is an element in the DOM tree,

cache[ "c" ] = $("<div/>");
for( n = cache["s"].length; n >= 0; n-- ){cache["n"].append(cache["c"]); }

seems not working, and I have no clue, why. It appends the div element ONCE instead of 9 times.

However, if I say,

cache[ "c" ] = "<div/>";
for( n = cache["s"].length; n >= 0; n-- ){cache["n"].append(cache["c"]); }

it works, I get 9 elements, but they are not properly inserted into the DOM tree.

What is the problem, how could I solve it without losing speed?

3
  • Do you mean cache[n], no quote? Commented May 21, 2014 at 13:20
  • Nope, "n" is the index in the cache array. Commented May 21, 2014 at 13:22
  • Ok, the var name are not very intuitive :) Commented May 21, 2014 at 13:23

2 Answers 2

1

cache["c"] is a single element, which mean that when you append it over and over, it doesnt create a new div but take the old one and move it. Use .clone():

cache["n"].append(cache["c"].clone());
Sign up to request clarification or add additional context in comments.

Comments

1
 cache[ "c" ] = $("<div/>");

Here cache[ "c" ] is a instance of div element and if you try to apend it more than one time it replaces its instance.

But if use cache[ "c" ] = "<div/>"; then it is appending new div element each time.

If you want to use cache[ "c" ] = $("<div/>"); then try appending its clone everytime.

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.