budzor
budzor

Reputation: 633

display table in IE

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?

Upvotes: 1

Views: 188

Answers (1)

Nick Craver
Nick Craver

Reputation: 630429

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.

Upvotes: 2

Related Questions