I am practicing some jQuery and have some issue that return the expected result. As I understand it, to target the selected element inside an event handler, we can pass the event to the callback function and use event.currentTarget
instead.
So this:
$('.nav-menu').on('mouseleave', ()=>{
$('.nav-menu').hide();
});
Would become this:
$('.nav-menu').on('mouseleave', (event)=>{
$(event.currentTarget).hide();
});
If I understand this link (W3Schools) correct, the same thing could be achieved using $('this')
:
$('.nav-menu').on('mouseleave', ()=>{
$(this).hide();
});
In this last case, the callback function does not need to be passed the event as parameter. It's also shorter, so that's cool :)
But it doesn't work. I'm not sure why. The first 2 solutions work fine.
Advice anyone? Thanks!