Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2008, 5:59 AM   #1
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
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
Old Mar 2nd, 2008, 8:39 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,035
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Higlight row using DOM

I remember trying to manipulate the styles of individual rows in tables, and I was running into similar problems.

I solved it by wrapping the <tr></tr> in a tbody, and then manipulating the tbody's class.

	<tbody class="mail"><tr>
		<td>Hello World</td>
		<td>Kruptof</td>
		<td>Today</td>
	</tr></tbody>

Hope that helps.

Also, enumerating each tr tag in your javascript is odd. Why not directly reference the tag you're looking for? And of course, you're going to need to update that code to look for the mail tbody, instead of the mail tr.

Oh, and your javascript can be substituted with something stabler, quicker, and more compatible: CSS. Unless you need to do more than alter the hover colour.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Mar 2nd, 2008, 10:33 AM   #3
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3 kruptof is on a distinguished road
Re: Higlight row using DOM

Quote:
Originally Posted by Sane View Post
I solved it by wrapping the <tr></tr> in a tbody, and then manipulating the tbody's class.
Just tried the wrapping and it still behaves the same. I think this is happening because the mouseover event is not being propagated to the cells ancestors. I found a work around which is to get the parent of the cell an set its classname:
elm.parentNode.className = "highlight"

Quote:
Originally Posted by Sane View Post
Oh, and your javascript can be substituted with something stabler, quicker, and more compatible: CSS. Unless you need to do more than alter the hover colour.
The reason I went with JS over CSS is because I find it some browsers don't all support the hover pseudo class for all elements especially IE.


Quote:
Originally Posted by Sane View Post
Also, enumerating each tr tag in your javascript is odd. Why not directly reference the tag you're looking for?
Please explain further, do you mean give each tr a unique id? If so wouldn't this be more code than now.
__________________
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
Old Mar 2nd, 2008, 12:54 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,035
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Higlight row using DOM

Quote:
Originally Posted by kruptof View Post
The reason I went with JS over CSS is because I find it some browsers don't all support the hover pseudo class for all elements especially IE.
Really? I've never heard of or encountered such issues with CSS and hovering. Maybe your CSS wasn't standard? I've been able to to all sorts of funky hovering in CSS. However, it does require that you assign ID's to each tbody.

Quote:
Originally Posted by kruptof View Post
Please explain further, do you mean give each tr a unique id? If so wouldn't this be more code than now.
Well you can give them all the same ID too. It's not "standard", but I've yet to encounter it causing any problems in any browser.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incompatible pointer type (double pointer in a function) Gabriel Margarido C 16 Nov 23rd, 2007 4:04 AM
can someone please help me with this tree cwl157 Java 19 Oct 27th, 2007 1:35 PM
more problems with copying from classes cwl157 Java 0 Sep 19th, 2007 5:17 PM
why does it change both instances cwl157 Java 10 Sep 19th, 2007 12:01 AM
data tree for A* algorithm cwl157 Java 0 Sep 17th, 2007 4:48 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC