In jQuery we are used to binding click handlers like this:
$(".all-buttons").click(function() {
var btn = this;
console.log(btn.innerHTML);
});
I would like to be able to use the same functionality with ES6 arrow functions like this:
$(".all-buttons").click(btn => console.log(btn.innerHTML));
The issue is that jQuery doesn't pass the current element as an argument like other native javascript functions do, for example Array.forEach()
Another issue is that arrow functions cannot be "applied" i.e. called like fn.apply(this, args)
, which is what jQuery relies on for letting me know which element triggered the event.
I just put the "click" example here, but I would like to be able to apply this to any jQuery callback.
Does anybody have any suggestions about how to achieve this? perhaps to use a different library that does call my callbacks with the element as an argument? or perhaps a jquery plugin? or a javascript trick I don't know about?
I appreciate the help.