Search code examples
javascriptjqueryhoverintent

Can I call my function in this example immediately instead of wrap it by anonymous function?


I don't know how to pass $(this) in direct call

var parentLI = $("#nav .parent");

parentLI.hoverIntent(function() {
    showUL($(this));
}, function() {
    hideUL($(this));
});

Solution

  • You can give the functions by reference:

    parentLI.hoverIntent(showUL, hideUL);
    

    Then in those functions you can refer to the element which raised the event by the this keyword:

    function showUL() {
       var $el = $(this);
       // do something with $el...
    }