How To Change Row Background Color OnMouseOver
Written by Dave   
Monday, 24 December 2007
I've seen on some sites, more specifically web applications, where when you put your mouse over a specific row of a list or table, that row would change color - like it was being highlighted. It took me forever to figure out how to do this.

I was accustomed to using JavaScript's getElementById() function. I was thinking when priting from a database, to give each row an ID also. So when you call onMouseOver() it would call another function which would then use getElementById() and change the back ground color of that specific row.

Well, now that I'm getting more and more used to all the functions out there, I've learned that you can use the 'this' keyword, as well as className to do this effect. Here's an example of what I'm talking about.

Row 1 Column 1 Row 1 Column 2
Row 2 Column 1 Row 2 Column 2
Row 3 Column 1 Row 3 Column 2
Row 4 Column 1 Row 4 Column 2
Row 5 Column 1 Row 5 Column 2
Row 6 Column 1 Row 6 Column 2

Now here's the code.

<style type="text/css">
table { color: #FFFFFF; font-family: Arial, Verdana }
.normal { background-color: #000000 }
.normalActive { background-color: #999999 }
</style>

<table class="ex" width="500" cellspacing="1"> <tr onmouseover="this.className='normalActive'" onmouseout="this.className='normal'" class="normal">
<td>Row 1 Column 1</td>
<td>Row 1 Column 2</td>
</tr>
<tr onmouseover="this.className='normalActive'" onmouseout="this.className='normal'" class="normal"> <td>Row 2 Column 1</td>
<td>Row 2 Column 2</td>
</tr>
<tr onmouseover="this.className='normalActive'" onmouseout="this.className='normal'" class="normal"> <td>Row 3 Column 1</td>
<td>Row 3 Column 2</td>
</tr>
<tr onmouseover="this.className='normalActive'" onmouseout="this.className='normal'" class="normal"> <td>Row 4 Column 1</td>
<td>Row 4 Column 2</td>
</tr>
<tr onmouseover="this.className='normalActive'" onmouseout="this.className='normal'" class="normal"> <td>Row 5 Column 1</td>
<td>Row 5 Column 2</td>
</tr>
<tr onmouseover="this.className='normalActive'" onmouseout="this.className='normal'" class="normal"> <td>Row 6 Column 1</td>
<td>Row 6 Column 2</td>
</tr>
</table>

So basically, all you need to do is create a normal class for when your mouse is not over the row, and then another class for when your mouse is over it. I hope this helps! I've been trying to get at this for awhile. I'm glad I figured it out. It really helps when you have lots of rows in a table and you start getting cross eyed.

Comments
Add NewSearchRSS
Iban (118.93.75.xxx) 2008-12-15 18:27:42

:woohoo: Thanks that was helpful for me.
Anonymous (118.93.75.xxx) 2008-12-15 18:28:28

Ty
Sibashis Das (122.173.179.xxx) 2009-02-19 23:33:50

Thanks to reveal this trick for all. Great job... It will be great if you share your knowledge with people like us who are just starting there career in IT World. :)
Anonymous (188.48.121.xxx) 2009-12-12 02:28:08

:huh: :kiss: :kiss: :kiss: Ajax request failed.
dev (59.180.156.xxx) 2010-01-29 08:15:21

Thanks
Write comment
Name:
Email:
 
Website:
Title:
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Image
Please input the anti-spam code that you can read in the image.




Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >

Polls

Would you like to see bigger implementation tutorials?
 

Get our E-Mail Newsletter:

 Subscribe in a reader