Equivalent of 'this' inside lambda function

Viewed 4880

I have a function like:

$(".myDropdown").hover(function() {
  $(this).find(".myDropdown-caretDown").hide();
}, function() {
  $(this).find(".myDropdown-caretDown").show();
});

If I want to use lambda function for hover callback, What can I use instead of this?

2 Answers

this inside arrow function is the same object as this in the scope that defines this function:

$(".myDropdown").hover(
  e => $(e.target).find(".myDropdown-caretDown").hide(),
  ...
});

What can I use instead of this?

Use event.target

$(".myDropdown").hover('click', (event) => {
    $(event.currentTarget).find(".myDropdown-caretDown").hide();
  },(event) => {
    $(event.currentTarget).find(".myDropdown-caretDown").show();
  },);
Related