createElement('div') into HTML table problem in IE 6.

M

markszlazak

Firefix works fine but I'm having a problem inserting a DIV into a
HTML table cell dynamically with IE 6. The following code has a two
cell table with one DIV hard-coded into the first (top) cell and a
second DIV put in via Javascript. The second DIV shows up like the
first in Firefox. However, in IE 6, only the text within the second
DIV shows but the DIV's style and positioning are lost. What is the
problem?

<html>
<head>
<style>
.grid {
}
.gridRow {
height:30;
}
.gridCell {
background-color:lightyellow;
width:50;
}

</style>
</head>
<body>
<table id='grid1' class='grid' border='1' cellpadding='0'
cellspacing='0'>
<tr class='gridRow'>
<td class='gridCell'>
<div
style= 'position:relative;
background-color:lightgrey;
border-width:1px;
border-color:black;
border-style:solid;
margin:0px;
padding:0px;
visibility:visible;
top:0;
left:10;
height:13;
width:28;
text-align:center;
font-size:10;
font-family:sans-serif;'
DIV</div>
</td>
</tr>
<tr class='gridRow'>
<td class='gridCell'></td>
</tr>
</table>
<script>
var grid = document.getElementById('grid1'),
cells = grid.getElementsByTagName('td');

var divStyle = 'position:relative; ' +
'background-color:lightgrey; ' +
'border-width:1px; ' +
'border-color:black; ' +
'border-style:solid; ' +
'margin:0px; ' +
'padding:0px; ' +
'visibility:visible; ' +
'top:0; ' +
'left:10; ' +
'height:13; ' +
'width:28; ' +
'text-align:center; ' +
'font-size:10; ' +
'font-family:sans-serif;';

function addCellDIV(el) {
var d = document.createElement('div');
d.setAttribute('style', divStyle);
d.appendChild(document.createTextNode('DIV'));
el.appendChild(d);
}
addCellDIV(cells[1]);

</script>
</body>
</html>
 
D

david.karr

Firefix works fine but I'm having a problem inserting a DIV into a
HTML table cell dynamically with IE 6. The following code has a two
cell table with one DIV hard-coded into the first (top) cell and a
second DIV put in via Javascript. The second DIV shows up like the
first in Firefox. However, in IE 6, only the text within the second
DIV shows but the DIV's style and positioning are lost. What is the
problem?
[deleted]
d.setAttribute('style', divStyle);

The following appears to be very relevant to this.

http://www.quirksmode.org/bugreport...te_does_not_work_in_IE_when_used_with_th.html
 
R

Robin Rattay


No DOCTYPE? Modern HTML documents should have a DOCTYPE to avoid the
trap holes of quirks mode. However, if you do add one, then you'll
need to correct the other errors in your document, otherwise it will
stop working in modern browsers - as it should.
<head>
<style>
.gridRow {
height:30;
}
.gridCell {
background-color:lightyellow;
width:50;
}

You need to validate your CSS (and HTML while you're at it). Length
values in CSS require a measurement unit such as "height: 30px".

And not all browsers may support non-standard color names such as
"lightyellow".

Also if all rows and cells in the table are using the same class, then
you can save some work by dropping the seperate grilRow and gridCell
classes und use a descendant selector:

.grid tr {
height:30px;
}
.grid td {
background-color:#FFFFE0;
width:50px;
}
<div
style= 'position:relative;
background-color:lightgrey;
border-width:1px;
border-color:black;
border-style:solid;
margin:0px;
padding:0px;
visibility:visible;
top:0;
left:10;
height:13;
width:28;
text-align:center;
font-size:10;
font-family:sans-serif;'

It would make sense to move this long list of styles to the
stylesheet, since you're reuseing it anyway.

The border properties can be shortened to "border: 1px solid black"
and the visibility, paddings and margins are all default for DIVs, so
they are probably not needed.

BTW, do you actually know what "position: relative;" does? Do you
really want the DIV to overlap any content (for example the table
border) to the right? If you just want to move the DIV to the right a
simple "margin-left: 10px" would be much better.
var grid = document.getElementById('grid1'),
cells = grid.getElementsByTagName('td');

Have a look at the rows and cells collections in a DOM reference. They
are usually the simpler and more reliable way to reference table
elements:

addCellDIV(grid.rows[0].cells[1]);
d.setAttribute('style', divStyle);

Here is probably your actual problem. setAttribute is very broken in
IE. It's best to avoid it:

d.style = divStyle;

Robin
 
T

Thomas 'PointedEars' Lahn

Robin said:
No DOCTYPE? Modern HTML documents should have a DOCTYPE to avoid the trap
holes of quirks mode.

DOCTYPE _declaration_
However, if you do add one, then you'll need to correct the other errors
in your document, otherwise it will stop working in modern browsers - as
it should.

It may as well not work now already.
You need to validate your CSS (and HTML while you're at it). Length
values in CSS require a measurement unit such as "height: 30px".

Iff the length is different from 0 (as here).
And not all browsers may support non-standard color names such as ^^^^^^^^^^^^^^^^^^^^^^^^
"lightyellow".

http://www.w3.org/TR/css3-color/#svg-color

You are right about the rest.
BTW, do you actually know what "position: relative;" does? Do you really
want the DIV to overlap any content (for example the table border) to the
right? If you just want to move the DIV to the right a simple
"margin-left: 10px" would be much better.

That moves the left margin of the element's canvas 10px to the right.
Here is probably your actual problem. setAttribute is very broken in IE.
It's best to avoid it:

d.style = divStyle;

It is best to avoid direct assignment to the `style' property, and to use
standards-compliant shortcut style properties instead:

d.style.position = "relative";
d.style.backgroundColor = "lightgrey";
...

Have a look at <http://hsivonen.iki.fi/wannabe/>. You may be looking into
a mirror.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top