views:

372

answers:

2

Using JS to set the background color of a TD is fine. But, setting the border color is problematic in FF 3.0.18 although IE 6 doesn't experience this. FF is problematic in that it requires the TD element to have an attribute style initialized to border-style: solid. Without that, setting border color of a TD won't work. Is this known bug?

How do I set the border color without having to set style attribute as well as the initialization value?

I know another trick of setting the class attribute instead of setting the border color directly. Is this an indication that somehow TD hates having its border color set dynamically? Is this known as well?

The problematic code is below (the goal is find out why setting the border color of simple truth 1 does not work while simple truth 3 works when I employ the trick described above):

<html>
<head>
<title>Quirks FF 3.0.18</title>
<style type="text/css">
table {
    border-collapse: collapse;
}
</style>
<script type="text/javascript">
function changeBgColor()
{
    document.getElementById('simple').style.backgroundColor='yellow';
    document.getElementById('simple2').style.backgroundColor='yellow';
    document.getElementById('simple3').style.backgroundColor='yellow';
}

function quirk(id)
{
    var x = document.getElementById(id);

    x.style.border = '2px solid red';
}
</script>
</head>
<body>
    <input type="button" onclick="changeBgColor()" value="Change background color"/>
    <input type="button" onclick="quirk('simple')" value="Change border color 1"/>
    <input type="button" onclick="quirk('simple2')" value="Change border color 2"/>
    <input type="button" onclick="quirk('simple3')" value="Change border color 3"/>
    <table>
    <tr><td id="simple">Simple truth 1</td></tr>
    </table>
    <table>
    <tr><td><span id="simple2">Simple truth 2</span></td></tr>
    <table>
    <tr><td id="simple3" style="border-style: solid">Simple truth 3</td></tr>
    </table>
</body>
</html>
+1  A: 

I believe it's a bug. As you've already indicated, the workaround is to set border-style to solid. You could add it to the stylesheet so that TD is always initialized:

TD { border-style: solid; }

Tran
Sure, I believe it is a bug. But, is this widely known? My googling the other day brought no good result.
Tadeus Prastowo
+1  A: 

it's not a bug, every decent browser will require you to set all three values for border - color, style (solid, dashed etc.) and pixel width. if one or more attributes are missing, then results can vary.

you need to set all three attributes in JS to display the border correctly. no initialization (CSS) value is required, though.

moreover, you can always employ setting the border attributes separately via border-width, border-style, border-color: http://www.comptechdoc.org/independent/web/cgi/javamanual/javastyle.html

dusoft
It is a bug since I have set all three values in the JavaScript function quirk(): x.style.border = '2px solid red';
Tadeus Prastowo