views:

166

answers:

4

Given the following html table and script shown below I am having a problem where the mouse leave event appears to fire right after the mouse enter, even if I don't move the mouse out of the row.

<script type="text/javascript" language="javascript">
    function highlightRows(iMainID) 
    {
        $('tr[mainid=' + iMainID+ ']').each(function() {
            if ($(this).attr('old') == undefined) {
                $(this).attr('old', $(this).css('backgroundColor'));
            }
            $(this).animate({ backgroundColor: "#FFFFCC" }, 500);
            $(this).mouseout(function() {
                if ($(this).attr('old') != undefined) {
                    $(this).animate({ backgroundColor: $(this).attr('old') }, 500);
                }
            });
        });        
    }
</script>
<table>
    <tr> 
      <td mainid="1" onmouseover='highlightRows(1)'><div>text</div></td>
      <td mainid="1" onmouseover='highlightRows(1)'><div>text</div></td>      
      <td mainid="2" onmouseover='highlightRows(2)'><div>text</div></td>
    </tr>
<table>
A: 

You probably want to do something like this:

function yourMouseOver(){...}
function yourMouseOut(){...}

with:

<td onmouseover="yourMouseOver()" onmouseout="yourMouseOut()">html</td>

Setting the onmouseout event every time the onmouseover event is fired is redundant.

Jeremy
+2  A: 

As Pointy and Jer both indicated, you should choose one model (JQuery) or the other (onWhatever in HTML), don't mix them.

Most likely, your double-entry has to do with the fact that you're subscribing multiple times to the same event. (If you do mouseover twice, you get two mouseout event handlers that will both be called.)

Also, watch out for the duplicate "mainid" values. That looks like a problem, and may be the cause of your issue.

John Fisher
I need the duplicate mainid values because I want to highlight multiple rows that contain the same mainid value.The only reason I used the onmouseenter attribute on the element was because my control gets rebuilt using ajax, so the document.ready method only sets up the events at the beginning
Jeremy
@Jeremy: You should consider using JQuery's "live()" features then. From what I can tell, it both performs better and lets you write cleaner code.
John Fisher
live()! Thanks, didn't know about that one.
Jeremy
+1  A: 

The jquery way to do it would be to just use hover, set in a $(document).ready function and like @pointy said forgo the onmouseover all together

$(document).ready(function(){
    $('tr').hover(function() {
       if ($(this).attr('old') == undefined) {
          (this).attr('old', $(this).css('backgroundColor'));
       }
       $(this).animate({ backgroundColor: "#FFFFCC" }, 500);
    }, function() {
       if ($(this).attr('old') != undefined) {
           $(this).animate({ backgroundColor: $(this).attr('old') }, 500);
       }
    });
});
munch
The problem I had with using document.ready was that my tables rows are in an update panel which updates on a timer, so when the document initially loads the events get set, but after the update panel updates the contents, the events get lost
Jeremy
+1  A: 

why not use the .hover?

$('tr[mainid=' + iMainID+ ']').hover(
        function()
        {
            $(this).addClass('hoverClass');
        },
        function()
        {
            $(this).removeClass('hoverClass');
        }
    );
Mark Schultheiss
I have to agree, the hover event makes it so easy.
ryanulit