views:

389

answers:

1

I have created a table in ASP MVC and populate it with data from a database. I have alternating row colors using the following code:

<% if (count % 2 == 0) { %>
    <tr class="offeven" onclick="this.className='onclick'" onmouseover="this.className='on'" onmouseout="this.className='offeven'">
<% } %>
<% else { %>
    <tr class="offodd" onclick="this.className='onclick'" onmouseover="this.className='on'" onmouseout="this.className='offodd'">
<%  } count++; %>

each class has a different background color. So what I want is this to happen. When a user goes to the page, they will see the table w/ alternating row colors and when a mouse hovers over every row, that row in question changes color. When they select a row, the row changes to a different color and stays, even after the mouse has gone beyond the scope of the row (so onmouseout doesn't function). Now I wrote a very javascript functions for my old website which used gridviews and I don't think that they will work in this case (at least w/out some modifications). How would I go about accessing a table's rows through javascript and doing what I need to get done? Thanks.

Edit: I added the :hover into the CSS and that works (in FF, not in IE6) in the sense that the clicked row doesn't lose its background color after the mouse leaves the scope of that row. But the last hurdle is that I need any row to change to its original background color when a new row is clicked, because at the moment, each row highlights, but doesnt de-highlight.

+2  A: 

You can do it using some jquery

$(”table tr:even”).addClass(”offeven″);

Add some CSS deceleration for instead of using onmouseover

.offeven:hover, .offodd:hover

Maybe a better cross-browser solution is mentioned in this post:

jQuery Alternating Gray Rows in a Table, and Highlight Table Row Mouseover Example

CD
N.B he will need to code js hover events for ie6
redsquare
Or use this kind of solution for IE6: http://snipplr.com/view/1912/internet-explorer-ie6-css-hover/(isn't IE6 dead already?)
CD
He can use this too: http://www.xs4all.nl/~peterned/csshover.html
CD
CD, i tried the cross-browser solution and it didn't work in IE.
dangerisgo