View Single Post
Old Mar 2nd, 2008, 4:59 AM   #1
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 327
Rep Power: 3 kruptof is on a distinguished road
Higlight row using DOM

I am trying to highlight a row in table when the user mouse overs the row. I am trying to do this by changing the className of the row to a CSS selector class which will change the background color. But instead of the whole row highlighting i am getting only cell in the row highling which is the one the mouse is over.

Below is the code for the page, had to put the style and scripts on the same page as the markup for the example and also i know this won't work across browsers because it's just an example:

<html>
<head>
	<style>
	.highlight
	{
		background-color:yellow;
	}
	</style>
</head>
<body>
<table>
	<tr class="header">
		<th>Subject</th>
		<th>From</th>
		<th>Date</th>
	</tr>
	<tr class="mail">
		<td>Hello World</td>
		<td>Kruptof</td>
		<td>Today</td>
	</tr>
</table>
</body>
<script type="text/JavaScript">
tr = document.getElementsByTagName("tr");
for(i=0;i<tr.length;i++)
{
	if(tr[i].className == "mail")
	{
		tr[i].addEventListener('mouseover',highlight,false);
	}
}

function highlight(e)
{
	var elm = e.target;
	elm.className = "highlight";
}
</script>
</html>
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote