views:

161

answers:

3

Hello,

The html code (with javascript) shown below works in all browsers except IE.

I recently learned that IE don't want to handle the getElementById and id codes.

Is somebody so kind to advise me, is there another way to get it work or is there a workaround code?

Thanks in advance, Erik

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>test</title>
<script type="text/javascript">
<!--
var color = new Object();

color["100"] = new Array("300", "400");

color["200"] = new Array("100", "300", "400");

color["300"] = new Array("100", "200");

color["400"] = new Array("200");

var colors = new Array("related");

function on(id)
{
for (var i=0; i<color[id].length; i++)
{
var el = document.getElementById("index_"+color[id][i]);
if (el)
{
el.setAttribute("class", colors[i%1]);
}
}
}

function off(id)
{
for (var i=0; i<color[id].length; i++)
{
var el = document.getElementById("index_"+color[id][i]);
if (el)
{
el.removeAttribute("class");
}
}
}
//-->
</script>

<style type="text/css">
<!--
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
line-height: 18px;
color: #000000;
text-decoration: none;
}
a:link,
a:visited {
color: #000000;
text-decoration: none;
}

a:hover,
a:active {
color: #FF0000;
text-decoration: underline;
}
a.related {
color: #FF0000;
text-decoration: none;
}
-->
</style>
</head>

<body>

<a href="#" id="index_100" name="index_100" onMouseOver="on(100)" onMouseOut="off(100)">aaa</a><br />
<br />
<a href="#" id="index_200" name="index_200" onMouseOver="on(200)" onMouseOut="off(200)">bbb</a><br />
<br />
<a href="#" id="index_300" name="index_300" onMouseOver="on(300)" onMouseOut="off(300)">ccc</a><br />
<br />
<a href="#" id="index_400" name="index_400" onMouseOver="on(400)" onMouseOut="off(400)">ddd</a>

</body>
</html>
A: 

Do the <a> elements even need a name attribute?

If not, then you would probably be better of without them, to reduce the 'noise' factor.

The problem may be, however, that the finder details of the markup is being generated by some kind of framework (Struts, ASP.NET) - and you don't have the ability to control whether you get a name attribute or not.

belugabob
+1  A: 

you are trying to use setAttribute() to set the "class". Although that is technically totally valid, IE has a bug with setAttribute() and won't set that.

use this for IE

el.setAttribute("className", colors[i%1]);
scunliffe
That will break for other browsers. Use el.className = … instead.
David Dorward
+2  A: 

el.removeAttribute("class");

That won't work. Avoid getAttribute/setAttribute/removeAttribute in IE, they aren't properly supported. IE before version 8 confuses attribute access with JS object property access, resulting in confusing errors when the attribute is named differently (class vs className) or the type of the property is different (boolean or integer properties where an attribute is always string).

Better (more readable and cross-browser compatible) is to use the DOM HTML properties:

el.className= '';

<a href="#" id="index_100" name="index_100"

No need for both ‘id’ and ‘name’ on a-elements; just set ‘id’ on its own.

bobince