Reputation: 197
Hi I've just started te jQuery,
I am trying sth like that:
$(document).ready(function(){
$newdiv = $('<div id="ball" />');
for(var i=0;i<100;i++){
$('body').append($newdiv);
}
});
I know that iteration part's not right.. But how do I append 100 divs in jquery?
Upvotes: 1
Views: 17317
Reputation: 44740
used class="ball"
as id should be unique, but you get the point, how to create 100 div
$(document).ready(function () {
var $newdiv;
for (var i = 0; i < 100; i++) {
$newdiv = $('<div class="ball" />').text(i);
$('body').append($newdiv);
}
});
Demo --->
http://jsfiddle.net/Uq2ap/
Upvotes: 7
Reputation: 55740
ID is supposed to be unique on your Page. So use class instead.
Next , if you var $newdiv = $('<div/>'
to create a div outside the for loop , it would only create a single instance of the div as it already is available on the page and cached.
So need to move the creation to inside the for loop
$(document).ready(function () {
for (var i = 0; i < 100; i++) {
var $newdiv = $('<div/>', {
"class": "ball",
text: 'hi'
});
$('body').append($newdiv);
}
});
Upvotes: 4