0

My jQuery script does not work when I have a skin applied to my gridview. Once I add in the skin I cannot find the tr anymore. I assume it has something to do with the way the tr gets constructed. Without the skin, the row is a clean

    <tr>data</tr>. 

But with the skin the row is now

<tr style=....>data</tr>

Here is my jQuery that does work when I do not have skin applied to the gv.

 $(document).ready(function () {
        $('tr').mouseover(function () {
            $(this).toggleClass('highlightrow');
                }).mouseout(function() {
                    $(this).removeClass('highlightrow');
                })
    });

2 Answers 2

1

The rows are found just fine, problem is that they have "hard coded" background color in their style so the background color in the class has no effect.

One way around this is to store the previous color then directly set the background color in mouseover and restore the previous color (to preserve the skin) in the mouse out event.

Code would look like:

$(document).ready(function () {
    $('tr').mouseover(function () {
        $(this).data("prev_color", $(this).css("background-color"));
        $(this).toggleClass('highlightrow').css("background-color", "yellow");
    }).mouseout(function() {
        $(this).removeClass('highlightrow').css("background-color", $(this).data("prev_color"));
    });
});

Live test case: http://jsfiddle.net/yahavbr/awEaP/1/

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! Works great. I just added $('tr:not(:first)').mouseover(function () { to avoid highlighting the header.
@IMAbev cheers, glad I could help and thanks for sharing this useful information! :)
fyi I had multiple gridviews on a page all with .gridlink class. tr:not(:first) only effects the first gridview. I changed to tr:not(:firstchild) and it highlighted only the rows on all gv's - not the headers
1

I bet it is due to style having higher priority than your css. How is your highlightrow defined? For example if you specify a background-color here and it is also in the tr's style, it gets ignored.

Maybe adding the !important clause could help:

.highlightrow
{
    background-color: Red !important;
}

3 Comments

that does work. The only down side (minor) is that it highlights the table head too. Never knew of the !important clause. I wonder about it's browser support?
@IMA here is explanation: stackoverflow.com/questions/189621/…
@IMAbev: about !important support: stackoverflow.com/questions/1330184/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.