I’m looking for a way to make a grid (for now using Table, but soon in div).
Let’s sais I click on the first cell (x,y = 1,-1) I want the background color to change for 2 cells width and 3 cells height. (Total of 6 cells changed)…
If it’s easier to do it using div, go ahead… using jQuery please! 🙂
I really don’t know how to do this and if someone can put me on the path or give me a code that should do it… or better, a tutorial XD…
I really appreciate your help, 100 times thanks
EDIT:
What i’m trying to do actually here is an invisible grod make a system comparable to rts-like games, where the building is transparent and follow the mouse but it’s attached to the grid when you move, and on clikc the bulding is droped (no transparency)… Explaining this just so you can have a little visual here.
The following is for div (as it’s the long term goal):
Firstly, I made a little markup that will be like a sort of table.
The html is:
With the following css:
So it has a table like display (it’s a bunch of row composed of cells, cells in a same column share a class).
To do what you want, it looks like you’ll need to associate a click function to each
.cell.To do so is easy, using
$(".cell").click(function(){});Now, it’s time to complete the function.
Now the next step is coloring the cell you’ll need to color. To add the coloration we’ll use a special class (this way we can change more things easily):
The hardest part is to select the 6 cells. Numerous way can be used (for example we could have a grid of id like A1, A2, B1, B2 and select them using id) and the efficiency/design depends heavily on the markup you’ll have for your divs.
The way I’d do that is the following:
Retrieving the class of the column of my cell :
Retrieving the parent of current div
var parent=$(this).parent();Making a jQuery object containing the 3 cells in the current colomn:
Note that the add function returns a new collection, thus we need to assign the return value.
Adding the 3 next cells to that object
listOfCell=listOfCell.add(listOfCell.next());listOfCell.addClass("clickedCell");And it’s over 🙂
A working example here: http://jsfiddle.net/KZFzd/1/
Note that:
Hope that helps.
EDIT:
In order to answer to your comment, the new fiddle to handle a specified size: http://jsfiddle.net/KZFzd/3/
I added two input that let you specify the size. You’ll probably need to change that in your code
:).I also added the class removal to clean the display.
So the two main change are that now, we’re using two
forloops to add cells. And the clicked cell is the top left corner of the rectangle.the first one:
It’s just iterating from one parent to another to reach the desired height. (and the first parent assignation is now the next one directly.
par=$(this).parent().next()the second one:
It’s just iterating to add the next elements to reach the desired width.
Note that:
listOfCellis already a 1*1 cellnext()toprev()in one loop or/and the other.