1

Help me using this fiddle

As you have seen in title I have two functions on a single element and I want to disable one while the other one runing.

function SetupClick() {
$("#Wp").toggle(function(){
    $("#Wp").animate({"width":"500"},"slow");
},function(){
    $("#Wp").animate({"width":"100"},"slow");
});
}

function SetupMouseover() {
$("#Wp").mouseenter(function(){
    $("#Wp").animate({"width":"500"},"slow");
        });
            $("#Wp").mouseout(function(){
    $("#Wp").animate({"width":"100"},"slow");
        });
}

$('#Ck').click(function() {
  SetupClick();
  // Optionally, save choice
  localStorage['userchoice'] = 'click';
});
$('#Me').click(function() {
  SetupMouseover();
  // Optionally, save choice
  localStorage['userchoice'] = 'mouseover';
});
Colin
  • 19
  • 1
  • 7

3 Answers3

1

Here is a link to disable an event handler, hope this helps

Best way to remove an event handler in jQuery?

Community
  • 1
  • 1
sferret
  • 361
  • 2
  • 13
1

user136etcetc provided a good link describing event namespacing in jQuery. Here's your example, using namespacing in practice:

http://jsfiddle.net/4vST2/

var aniMinWidth = 100;
var aniMaxWidth = 500;

function SetupClick() {
    // Remove mouse events first
    $("#Wp").off("mouseout.bobsyouruncle");
    $("#Wp").off("mouseenter.bobsyouruncle");

    $("#Wp").on("click.bobsyouruncle", function() {
        var animateWidth = $(this).width() > aniMinWidth ? aniMinWidth : aniMaxWidth;

        $(this).animate({"width":animateWidth}, "slow");
    });  
}

function SetupMouseover() {
   // Remove click event first
  $("#Wp").off("click.bobsyouruncle");

  $("#Wp").on("mouseenter.bobsyouruncle", function(){
        $("#Wp").animate({"width":aniMaxWidth},"slow");
   });

   $("#Wp").on("mouseout.bobsyouruncle", function(){
        $("#Wp").animate({"width":aniMinWidth},"slow");
    });
}

$('#Ck').click(function() {
  SetupClick();
  // Optionally, save choice
  localStorage['userchoice'] = 'click';
});
$('#Me').click(function() {
  SetupMouseover();
  // Optionally, save choice
  localStorage['userchoice'] = 'mouseover';
});
zincorp
  • 3,254
  • 1
  • 13
  • 18
  • But I have one last question. You must click one of those two buttons to choose how to react the div, but before that I wan't to make mouseenter the default. How can I do that? – Colin Jun 20 '13 at 17:52
  • Using jQuery's page init binding, you would simply call SetupMouseover(). See the addition at http://jsfiddle.net/4vST2/1/ – zincorp Jun 20 '13 at 17:54
0

I don't think you can disable an event handlers, but you could set a variable to the name of the function that you want to execute and then test for it in each of the functions and act accordingly.

var MYNAMESPACE = {}
MYNAMESPACE.activeFunction = 'click'

function SetupMouseover() {
  if MYNAMESPACE.activeFunction != 'mouseover') {return}
  // rest of code here
}

Using $(selector).off() actually removes the click handler. If you want to use that, then you will have to toggle between the two event handlers.

MYNAMESPACE.toggleHandlers = function(h) {
  if (h === 'click') {
    removeMouseover();
    SetupClick();
  }
  else if (h === 'mouseover') {
    removeClick();
    SetupMouseover();
  }
}
Mark Anderson
  • 131
  • 1
  • 6