Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery pass this to function

I need to send this to the capIn() and capOut() functions so that I can target the children divs of the correct .slide. How do I pass this to a function. this would be what is hovered.

$(".slide").hover(function(){capIn();capOut();});
like image 259
Jonathan Eckman Avatar asked Nov 29 '22 15:11

Jonathan Eckman


2 Answers

Looking at the function names capIn and capOut it makes sense that they are 2 different behaviors. I believe you have 2 different behaviors on mouseenter and mouseleave events. hover method can take 2 methods, one for mouseenter and another for mouseleave. You can try this

$(".slide").hover(capIn, capOut);

You can use this inside capIn and capOut it will point to .slide element which you hovered on.

function capIn(){
   var $childrens = $(this).children();
}

function capOut(){
   var $childrens = $(this).children();
}
like image 108
ShankarSangoli Avatar answered Dec 10 '22 20:12

ShankarSangoli


$(".slide").hover(function(){
    capIn.apply(this);
    capOut.apply(this);
});

See here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply

UPDATE:

If capIn is for mouseenter and capOut is for mouseleave, then:

$(".slide").hover(function(){
    capIn.apply(this);
}, function(){
    capOut.apply(this);
});

ShankarSangoli's solution is more succinct, but if any arguments need to be passed in addition to this, then: capIn.apply(this, arguments) can be used.

like image 29
dgilland Avatar answered Dec 10 '22 21:12

dgilland