Reputation: 1
I need to create a grid of DIVs (all DIVs will have same dimensions) and set them a defined names, colors, positions etc. Which is the most relevant/easiest/fastest method of doing this according to you?
Any answers will be appreciated!
Upvotes: 0
Views: 2157
Reputation: 6366
The other answer is correct, but I prefer the jQuery element creation syntax:
for (var i = 0; i <= 10; i++) {
$('<div />', {
'class' : 'sameDiv',
'id' : 'div' + i
}).appendTo('body');
}
Fiddle : http://jsfiddle.net/K5ERR/
Upvotes: 2
Reputation: 74738
No doubt iterator in javascript like most used and most favorite for(){}
loop is fine and in terms of jQuery .each()
is what you are looking for.
using for loop with jQuery:
for(var i = 0; i<=10; i++){
$('<div />').addClass('sameDiv').attr('id','div'+i).appendTo('body');
}
Upvotes: 2
Reputation: 10830
An iterator (for loop would work) that uses the append method to add a bunch of divs.
If each one has slight differences, track the differences in a map or something the iterator can also access.
Upvotes: 2