*michael* wrote in comp.lang.javascript
:
I'm changing colors of table cells like this:
<style type="text/css">
.over {background-color: #bedddc}
.out {background-color: #99cccc}
</style>
[snip]
<td onmouseover="this.className='over'" onmouseout="this.className='out'">
[snip]
But what I want is a generic javascript to change the current
onmousover/out cell from a script above
the table instead of using "this.className" within each td code
[snip]
I see Yann-Erwan has already replied, however, I was playing around with
the following and thought it might be of some use still (I'm not sure of
it's general suitability - although it does work in IE5.0+, Opera7,
Mozilla/Firefox):
<script type="text/javascript">
function Init() {
AddMouseOverEffect("rootId", "td"); // Your id and tag name here
}
function AddMouseOverEffect(strId, strTag) {
if (document && document.getElementById && document.getElementsByTagName) {
var objRoot = document.getElementById(strId);
if (objRoot) {
var objChildTags = objRoot.getElementsByTagName(strTag);
var intIndex = objChildTags.length;
while (intIndex--) {
var objTag = objChildTags[intIndex];
if (objTag.attachEvent) {
objTag.attachEvent('onmouseover', Effect_MouseOn);
objTag.attachEvent('onmouseout', Effect_MouseOff);
}
else if (objTag.addEventListener) {
objTag.addEventListener("mouseover", Effect_MouseOn, false);
objTag.addEventListener('mouseout', Effect_MouseOff, false);
}
}
}
}
}
function Effect_MouseOn(evnt) {
var context = evnt.srcElement ? evnt.srcElement : this;
if (context && context.style) {
with (context.style) {
backgroundColor = "#bedddc";
color = "#000000";
}
}
return false;
}
function Effect_MouseOff(evnt) {
var context = evnt.srcElement ? evnt.srcElement : this;
if (context && context.style) {
with (context.style) {
backgroundColor = "#99cccc";
color = "#000000";
}
}
return false;
}
if (window.attachEvent) {
window.attachEvent('onload', Init);
}
else if (window.addEventListener) {
window.addEventListener("load", Init, false);
}
</script>
<table id="rootId">
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
<td>Cell 1/4</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
<td>Cell 2/4</td>
</tr>
</table>
--
Andrew Urquhart
- FAQ:
http://www.jibbering.com/faq/
- Archive:
http://www.google.com/groups?q=comp.lang.javascript
- Contact me:
http://andrewu.co.uk/contact/