S
S
Sergey Kotov2019-10-26 17:05:57
macbook
Sergey Kotov, 2019-10-26 17:05:57

Why does append not work correctly?

This code doesn't work properly (doesn't output 5 times "lol'")
<div id="render"></div>

var tohandler = $('<div></div>').text('lol'), 
    container = $('<div></div>').addClass('container');
for(var i = 0; i<5; i++){
  container.append(tohandler);
}
$('#render').html(container);


And this one works as it should (outputs 5 times "lol'") what's the difference?
<div id="render"></div>
var container = $('<div></div>').addClass('container');
for(var i = 0; i<5; i++){
  container.append("<div>lol</div>");
}
$('#render').html(container);

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
GavriKos, 2018-08-28
@GavriKos

In addition to a small monitor, it will do.

A
Andrew, 2018-08-28
@KeySVG

Of course, I have a 2011 home iMac with almost the same specs, except maybe a 2.7 quad-core i5. On it, production has already been done for more than one hundred thousand! And he's not going to retire yet.
The only thing is that if 13 ”is not enough - take a larger external monitor, keyboard and trackpad are also external.

O
OnYourLips, 2018-08-28
@OnYourLips

My requirement is 16GB of RAM and an SSD. This one would suit me.

L
lamer350, 2018-08-28
@lamer350

The percentage is weak, for example phpStorm - it will generally be stupid.
On my 13th even Sublime 3 was stupid, or rather not the editor itself - namely the colorhighlighter plugin, when css is 2k lines, then writing some kind of color is hell) Wrote #fff; and wait until the characters appear on the screen, about 1 character in 10 seconds)
Therefore, if you take - only 4 nuclear.

C
Chipekwe, 2019-10-26
@liqrizz

Because the tohandler variable is created once and you run it five times.
You need to create it inside the loop.

var   container = $('<div></div>').addClass('container');
for(var i = 0; i<5; i++){
var tohandler = $('<div></div>').text('lol');
  container.append(tohandler);
}
$('#render').html(container);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question