Carlos Bernardes
Carlos Bernardes

Reputation: 541

How to access rows in table generated in javascript

I've generated a table in my html by using this code:

 var board=document.getElementById("tab");

    for(var i=0; i<lvl1.rows; i++ )
    {

            var row=board.insertRow();

            for(var j=0; j<lvl1.cols; j++)
            {
                var cell = row.insertCell();       
            }       
    }

The point is to keep the design of the page almost totally separated from the game engine (creating a Minesweeper game). Imagine I want to change the colour of the cell in position [2][3]. How can I change the background colour of this cell if I don't have the "td's" and "tr's" in the HTML code?

Thanks

Upvotes: 2

Views: 85

Answers (2)

eirikjak
eirikjak

Reputation: 76

You can set the background color style of a cell like this.

board.rows[j].cells[i].style.backgroundColor = "red";

Upvotes: 0

Alnitak
Alnitak

Reputation: 339786

To access the ith cell of row j, use:

board.rows[j].cells[i]

Upvotes: 2

Related Questions