views:

26

answers:

1

I have code like that:

var xPola = 10, //how many cols
    yPola = 10, //how many cols
    bokPola = 30, //size of cell
    body = document.getElementsByTagName('body')[0];


var tablica = document.createElement('table');
body.appendChild(tablica);

for( var y = 0; y < yPola; y++ ) {
    var rzad = document.createElement('tr');
    tablica.appendChild(rzad);
    for( var x = 0; x < xPola; x++ ) {
        var pole = document.createElement('td');
        pole.setAttribute('width', bokPola);
        pole.setAttribute('height', bokPola);
        rzad.appendChild(pole);
    }
};

it works fine in FF, Chrome & Opera (it displays 10x10 table with 30px width&height rows). In IE nothing happens. I check in firebug lite and it is in HTML section, inside BODY tag but i see nothing. Whats wrong?

+1  A: 

It needs a few tweaks:

var xPola = 10, //how many cols
    yPola = 10, //how many cols
    bokPola = 30, //size of cell
    body = document.getElementsByTagName('body')[0];


var tablica = document.createElement('table');
body.appendChild(tablica);
var tbody = document.createElement('tbody');
tablica.appendChild(tbody);

for( var y = 0; y < yPola; y++ ) {
    var rzad = document.createElement('tr');
    tbody.appendChild(rzad);
    for( var x = 0; x < xPola; x++ ) {
        var pole = document.createElement('td');
        pole.innerHTML='&nbsp;';
        pole.style.width = pole.style.height = bokPola;
        rzad.appendChild(pole);
    }
}

IE needs a <tbody> in most cases, and wont' display empty cells, so it needs a space in there.

Nick Craver
THANKS! i was looking everywhere 4 anwser
budzor
i also have to add for each cell some onclick action, i make it in FF like this: pole.setAttribute('onclick', 'javascript:FunctionName();'); how to do it in IE?
budzor
@budxor for both just do `pole.onclick = FunctionName;`
Nick Craver
it doesn't work.
budzor
@budzor - Works here across the board, can you update or ask another question with the code you're trying included?
Nick Craver