Search code examples
javascriptjquerydelegatesjquery-events

How to give two functions to delegate function


I'm using JQuery and trying to use delegate for the hover action. Problem is the hover action can get two handlers, the handle in and the handle out. How can I achieve this using delegate?

I've tried this and it didn't work:

 $(document).delegate('.box', 'hover',  
    function() { $(".a").addClass(".hover");}, 
    function() { $(".a").removeClass(".hover");});

Solution

  • According to the docs for .hover:

    $(selector).mouseenter(handlerIn).mouseleave(handlerOut);
    

    So you should be able to just call delegate once for each of these functions:

    $(document)
      .delegate('.box', 'mouseenter', function() { alert(1); })
      .delegate('.box', 'mouseleave', function() { alert(2); });