aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes jquery, hover and unbind Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jquery, hover and unbind" Watch "jquery, hover and unbind" New topic
Author

jquery, hover and unbind

jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
Hi,

I'm facing a real problem, it's about the hover function in jquery. Whenever I call .hover(f(e), g(e)) the functions f(e) and g(e) somehow pileup. I guess this is normal behaviour and can be undone with
$(this).unbind('mouseover').unbind('mouseout');
However when i immediately do after the unbind:
$(this).hover(function(e){..}, function(){}) ;
The functions f(e), and (probably) g() simply dont get evoked after the hover event. Is there something I'm missing? I'm a bit lost. Any help is greatly appreciated.

kind regards,
Jeroen dijkmeijer.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Are you calling hover() more than once? I'm not sure what you mean by "piling up".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
onmouseover and onmouseout get called if there are children elements and the mouse enters and leaves them.

Eric

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

But not with hover(). That's the whole point of the method.
jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
Thanks for the answers so far and my apologies for the unthoughtful asked question. Below a code snippet which should clarify a bit.


with line 13 commented, out clicking the "click me" (why cant this be an <a href=.. btw?) will add functions to the mouseover and mouseout event of the red box. So without a click nothing happens, first click will nicely alert the count on the mouseout. The count is increased by one with every move into the red box. Second click will show alert 2 times and the count will be increased by 2, a third click will alert 3 times and delta will be three. This behavior is undesired, I only want two functions on the hover which will be replaced every time I hit "click me"
So I thought to be clever and I uncommented line 13, which I hoped, would remove the existing function from the mouseover and mouseout events and attach new functions to it. But that only works first time, second time I hit "click me" entering and leaving the red box doesn't do a thing.
Where did I go wrong? And what can I do to replace the functions on the "hover" instead of "piling them up"?

regards,
Jeroen Dijkmeijer.

PS This example does not show the need for the replacement of functions in the hover element, but in my real project, that need certainly exists!
PPS safari and firefox give same results.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Bear said what the problem was, guess his magic ball was working. If you click click me a 2nd time you are adding the hover event for the second time.

You either need to check that it is attached or you need to unbind hover before applying it once again.

Eric

jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
Haa!

Found it! Explaining the question is already 80% of the answer and using the development version of jquery made it completely clear:
instead of the googled mouseover and mouseout I should use: "mouseenter" and "mouseleave". Now only convince Google that this is the solution, so others don't run into the same problem.

remains the question why binding a function to the click event does not work for the <a href=...

regards,
Jeroen.

jeroen dijkmeijer
Ranch Hand

Joined: Sep 26, 2003
Posts: 131
You either need to check that it is attached or you need to unbind hover before applying it once again.

For the record unbind('hover') does not work, and for the example I worked with there is no need to check whether hover was attached.
 
Don't get me started about those stupid light bulbs.
 
subject: jquery, hover and unbind