Stromovous
Stromovous

Reputation: 1

Creating grid of DIVs using jQuery

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

Answers (3)

couzzi
couzzi

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

Jai
Jai

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');
}

CHECK THIS OUT

Upvotes: 2

Greg Pettit
Greg Pettit

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

Related Questions