Forum Moderators: open
<div class="servicetab" style="margin: 0 4px 0 0;" onMouseOver="document.procurement.className='svc1picon'" onMouseOut="document.procurement.className='svc1pic'">
<div class="svc1head"></div>
<div id="procurement" class="svc1pic" ></div>
<div class="svcbody">
Hello<br/><br/><br/><br/><br/>
</div>
<a href="" target="_self" title="IT Procurement"><div class="svc1info"></div></a>
</div>
This isn't working though, don't seem to be able to target the element properly.
I can change the class easily if I put the mouseOver statement in the div which I want to change and do it this way:
<div class="servicetab" style="margin: 0 4px 0 0;" >
<div class="svc1pic" onMouseOver="this.className='svc1picon'" onMouseOut="this.className='svc1pic'"></div>
<div class="svcbody">
Hello<br/><br/><br/><br/><br/>
</div>
<a href="" target="_self" title="IT Procurement"><div class="svc1info"></div></a>
</div>
How do I achieve this effect?
But, using childNodes, worked fine for me:
<div class="servicetab" style="margin: 0 4px 0 0;" onMouseOver="this.childNodes[3].className='svc1picon'" onMouseOut="this.childNodes[3].className='svc1pic'">
<div class="svc1head"></div>
<div class="svc1pic"></div>
<div class="svcbody">
Hello<br/><br/><br/><br/><br/>
</div>
<a href="" target="_self" title="IT Procurement"><div class="svc1info"></div></a>
</div>
By the way, welcome to WebmasterWorld!
[edited by: Birdman at 2:05 pm (utc) on Feb. 20, 2007]
As a quick test, I would remove all newlines (put the whole parent div code on one line) and then try it. Obviously, this isn't a long term fix though. Probably should consider using the getElementById approach after all. I'm sure it must work.
Sorry, I have that backwards. Mozilla interprets the whitespace as nodes, not IE. This explains to me why I had to use chilNodes[3] with my Konqueror browser.
[edited by: Birdman at 6:08 pm (utc) on Feb. 20, 2007]
(put this in the <head> of your page)
<script type="text/javascript">
function classchange(newclassname){
document.getElementById("test").className=newclassname;
}
</script>
Then:
<div class="servicetab" style="margin: 0 4px 0 0;" onMouseOver="classchange('svc1picon')" onMouseOut="classchange('svc1pic')">
<div class="svc1head"></div>
<div id="test" class="svc1pic"></div>
<div class="svcbody">
Hello<br/><br/><br/><br/><br/>
</div>
<a href="" target="_self" title="IT Procurement"><div class="svc1info"></div></a>
</div>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<style type="text/css">
#servicetab {
margin: 0 4px 0 0;
}
.svc1picon { background-color: #00f; color: #f00; }
.svc1pic { background-color: #f00; color: #00f; }
</style>
<title>Unobtrusive Example</title>
<script type="text/javascript">
function changeClass( idref, classname ) {
var el = document.getElementById(idref);
var attributeNode = el.getAttributeNode("class");
if( attributeNode ) { attributeNode.value = classname; }
else { el.setAttribute("class", classname); }
}
window.onload = function(){
var serviceTab = document.getElementById('servicetab');
serviceTab.onmouseover = function(){changeClass('procurement','svc1picon');};
serviceTab.onmouseout = function(){changeClass('procurement','svc1pic');};
}
</script>
</head>
<body>
<div id="servicetab">
<div class="svc1head"></div>
<div id="procurement" class="svc1pic" >Test</div>
<div class="svcbody">
Hello<br/><br/><br/><br/><br/>
</div>
<!-- This next part is not valid... can't have a block level elment within an
inline element (the link) so I commented it out -->
<!--
<a href="" target="_self" title="IT Procurement"><div class="svc1info"></div></a>
-->
</div>
</div>
</body>
</html>
however I've just given each a unique id and defined each in the js
Ouch! Do I smell code-bloat ;)
Here's the solution that I was working on before but never finished. It doesn't need any ids at all, so may be repeated as many times as you like:
[pre]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<style type="text/css">
.svc1picon { background-color: #00f; color: #f00; }
.svc1pic { background-color: #f00; color: #00f; }
</style>
<title>Unobtrusive AND lightweight Example</title>
<script type="text/javascript">
window.onload = function(){
var serviceTab = document.getElementsByTagName('div');
for (i in serviceTab){
if(serviceTab[i].className == 'servicetab'){
serviceTab[i].onmouseover = function(){
var nodes = this.childNodes; for (e in nodes) if (nodes[e].className == 'svc1pic') nodes[e].className ='svc1picon'
}
serviceTab[i].onmouseout = function(){
var nodes = this.childNodes; for (e in nodes) if (nodes[e].className == 'svc1picon') nodes[e].className ='svc1pic'
}
}
}
}
</script>
</head>
<body>
<div class="servicetab">
<div class="svc1head"></div>
<div class="svc1pic" >Test</div>
<div class="svcbody">Hello 1</div>
</div>
<div class="servicetab">
<div class="svc1head"></div>
<div class="svc1pic" >Test</div>
<div class="svcbody">Hello 2</div>
</div>
<div class="servicetab">
<div class="svc1head"></div>
<div class="svc1pic" >Test</div>
<div class="svcbody">Hello 3</div>
</div>
<div class="servicetab">
<div class="svc1head"></div>
<div class="svc1pic" >Test</div>
<div class="svcbody">Hello 4</div>
</div>
<div class="servicetab">
<div class="svc1head"></div>
<div class="svc1pic" >Test</div>
<div class="svcbody">Hello 5</div>
</div>
<div class="servicetab">
<div class="svc1head"></div>
<div class="svc1pic" >Test</div>
<div class="svcbody">Hello 6</div>
</div>
</body>
</html> [/pre]
[edited by: Birdman at 10:48 pm (utc) on Feb. 20, 2007]
<!-- SERVICE -->
<div class="servicetab" id="svc1" style="margin: 0 4px 0 0;">
<div class="svc1head"></div>
<div class="svc1pic" id="svcprocurement"></div>
<div class="svcbody"><p>Hello</p><p>Hello</p></div>
<div class="svc1info" onclick="location.href='somewhere.php';" style="cursor:pointer;" ></div></div><!-- SERVICE -->
<div class="servicetab" id="svc2" style="margin: 0 4px 0 0;">
<div class="svc2head"></div>
<div class="svc2pic" id="svcsolutions"></div>
<div class="svcbody"><p>Hello</p><p>Hello</p></div>
<div class="svc2info" onclick="location.href='somewhere.php';" style="cursor:pointer;" ></div></div>
<!-- SERVICE -->
<div class="servicetab" id="svc3" style="margin: 0 4px 0 0;">
<div class="svc3head"></div>
<div class="svc3pic" id="svcmanservices"></div>
<div class="svcbody"><p>Hello</p><p>Hello</p></div>
<div class="svc3info" onclick="location.href='somewhere.php';" style="cursor:pointer;" ></div></div>
<!-- SERVICE -->
<div class="servicetab" id="svc4" style="margin: 0 4px 0 0;">
<div class="svc4head"></div>
<div class="svc4pic" id="svcsupport"></div>
<div class="svcbody"><p>Hello</p><p>Hello</p></div>
<div class="svc4info" onclick="location.href='somewhere.php';" style="cursor:pointer;" ></div></div>
<!-- SERVICE -->
<div class="servicetab" id="svc5" style="margin: 0 4px 0 0;">
<div class="svc5head"></div>
<div class="svc5pic" id="svcdisposal"></div>
<div class="svcbody"><p>Hello</p><p>Hello</p></div>
<div class="svc5info" onclick="location.href='somewhere.php';" style="cursor:pointer;"></div></div>
<!-- SERVICE -->
<div class="servicetab" id="svc6">
<div class="svc6head"></div>
<div class="svc6pic" id="svcleasing"></div>
<div class="svcbody"><p>Hello</p><p>Hello</p></div>
<div class="svc6info" onclick="location.href='somewhere.php';" style="cursor:pointer;"></div></div>
and a js
function changeClass( idref, classname ) {
var el = document.getElementById(idref);
var attributeNode = el.getAttributeNode("class");
if( attributeNode ) { attributeNode.value = classname; }
else { el.setAttribute("class", classname); }
}
window.onload = function(){
var svc1 = document.getElementById('svc1');
svc1.onmouseover = function(){changeClass('svcprocurement','svc1picon');};
svc1.onmouseout = function(){changeClass('svcprocurement','svc1pic');};
var svc2 = document.getElementById('svc2');
svc2.onmouseover = function(){changeClass('svcsolutions','svc2picon');};
svc2.onmouseout = function(){changeClass('svcsolutions','svc2pic');};
var svc3 = document.getElementById('svc3');
svc3.onmouseover = function(){changeClass('svcmanservices','svc3picon');};
svc3.onmouseout = function(){changeClass('svcmanservices','svc3pic');};
var svc4 = document.getElementById('svc4');
svc4.onmouseover = function(){changeClass('svcsupport','svc4picon');};
svc4.onmouseout = function(){changeClass('svcsupport','svc4pic');};
var svc5 = document.getElementById('svc5');
svc5.onmouseover = function(){changeClass('svcdisposal','svc5picon');};
svc5.onmouseout = function(){changeClass('svcdisposal','svc5pic');};
var svc6 = document.getElementById('svc6');
svc6.onmouseover = function(){changeClass('svcleasing','svc6picon');};
svc6.onmouseout = function(){changeClass('svcleasing','svc6pic');};
}
I'm sure there probably is a more efficient way through this