
Originally Posted by
ralph.m
OK, what you are trying to do here is affect a parent element on the basis of a child, which you can't really do with CSS, so you may have to stick with JS. I'll move this thread to the JS forum.
Thanks Ralph.
Dante, what you can do is to remove the inline HTML attributes for the events, and move them out to where they belong as scripting code.
We'll put an id attribute on each td, just for the purposes of testing this. We should be able to get rid of most of them by the time we finish this.
HTML Code:
<td id="first"><a href="#">Highlight a link on mouseover</a></td>
<td id="second">Highlight a cell on mouseover</td>
<td id="third"><a href="#">Now highlight a cell when I hover over it -> working.<br />...</a></td>
At the end of the body, just before the </body> tag, is where we put the script. That allows the script to easily work with elements on the page before the page is loaded.
HTML Code:
<body>
...
<script type="text/javascript" src="js/script.js"></script>
</body>
The script.js file will contain the scripting that we do.
In that script we want to gain a reference to each of the parts you're testing there, and to set the mouseover background. Because that's a behaviour that will be common to several elements on the page, we'll use a single function to set up each element.
Code javascript:
function initMouseoverBackground(el, color) {
el.onmouseover = function () {
this.style.backgroundColor = color;
};
el.onmouseout = function () {
this.style.backgroundColor = '';
};
}
Notice how on mouseout we're not setting the color to white, but are clearing the backgroundColor setting? This is because javascript is creating a whole new set of CSS declarations in a style attribute on the element, so removing the backgroundColor setting from that inline style attribute allows the element to revert back to what it was before.
Now we can use that initMouseoverBackground function to set up the mouseover behaviour for each element.
Code javascript:
var el = document.getElementById('first').getElementsByTagName('a')[0];
initMouseoverBackground(el, 'yellow');
el = document.getElementById('second');
initMouseoverBackground(el, 'yellow');
el = document.getElementById('third');
initMouseoverBackground(el, 'yellow');
var el = document.getElementById('third').getElementsByTagName('a')[0];
initMouseoverBackground(el, 'yellow');
That works on all but the third section link, because according to the way that mouseover works, it considers that you're also over all the parent elements of that link as well, which includes the td, tr, table and body elements. When you hover over the link, the mouseover event bubbles up from the link all the way up.
You want only the link to be highlighted, so we should cancel the event from bubbling further up so that it doesn't trigger the hover on the td itself.
We can do that by adding a parameter for the event (getting the event from window.event if we're on Internet Explorer) and cancelling the bubbling of the event.
Code javascript:
el.onmouseover = function (evt) {
evt = evt || window.event;
evt.cancelBubble = true;
this.style.backgroundColor = color;
};
With that modification in place things now work as you seem to prefer.
I'll follow this post with one that removes the multiple identifiers and sets up mouseover event for every element within the table.
Bookmarks