David
David

Reputation: 844

unbind a jquery hover event handler within click

I have rotating jQuery tabs which I want to pause when you hover over them and stop rotating when you click.

However, although the pausing works fine, I cannot unbind the hover event (even unbinding mouseenter and mouseleave separately).

I've created two jsfiddles here with two different approaches, both of which have the same problem.

http://jsfiddle.net/bdrvC/15/

function tab_hover_in() {
        $(this).tabs('rotate', 0, false);
}
function tab_hover_out() {
        $(this).tabs('rotate', 3000, false);
}
function tab_click() {
        $(this).tabs('rotate', 0, false);
        $(this).unbind('mouseenter',tab_hover_in);
        $(this).unbind('mouseleave',tab_hover_out);
        event.preventDefault();  
}

$('.tabs-rotate').tabs({
        selected: 'tabs-1'
}).tabs('rotate', 3000, false);

$('.tabs-rotate').bind({
        'click': tab_click,
        'mouseenter': tab_hover_in,
        'mouseleave': tab_hover_out
});

http://jsfiddle.net/bdrvC/16/

$('.tabs-rotate').tabs({
  selected: 'tabs-1'
}).tabs('rotate', 3000, false);

$('.tabs-rotate').hover(function() {
    $(this).tabs('rotate', 0, false);
  }, function() {
    $(this).tabs('rotate', 3000, false);
});
$('.tabs-rotate').click(function() {

  $(this).tabs('rotate', 0, false);
  $(this).unbind('mouseleave');

});

Could anyone explain why the rotation continues even after the click?

Many thanks!

Upvotes: 0

Views: 482

Answers (1)

malificent
malificent

Reputation: 1441

your click event isn't getting bound to the proper element...you could use

$('.ui-tabs-nav li a').click(function() {...});

instead

Upvotes: 1

Related Questions