When you do var $node = $("<div></div>"); you're creating a jQuery object with a newly-created div inside it which isn't in the DOM yet. The first time you append it, you put it in the DOM. The second time you append it, you move it. (Well, you would if you were appending it to something else, but actually you're just putting it in the same place.)
If you want multiple divs, you need to create more than one. clone does that, but in that code, you could just do it directly.
Append is perfectly happy with markup:
$( document ).ready(function() {
var $divg = $('#colors');
for (var i = 1; i <= 40; i++) {
$divg.append("<div></div>");
}
});
Or if it's important to create the element first because you're doing things with it:
$( document ).ready(function() {
var $node;
var $divg = $('#colors');
for (var i = 1; i <= 40; i++) {
$node = $("<div></div>");
$divg.append($node);
}
});
Or using clone, but in this case I probably wouldn't:
$( document ).ready(function() {
var $node = $("<div></div>");
var $divg = $('#colors');
for (var i = 1; i <= 40; i++) {
$divg.append($node.clone());
}
});
(Note that technically, that creates an extra div, but it's fine, that isn't expensive and it doesn't get added to the DOM anywhere.)
idattribute in a HTML document page. Use.colorsand see if that works.$divg.append($node.clone());. That's said, regarding performance, your code isn't optimized for sure