Say we have a simple event button handler:
$( \"#aButton\" ).click(function() {
console.log( \"tile\" + this.val() + \"clicked\" );
});
I re
Use $(this)
to obtain the clicked element.
$("#aButton").click(function() {
console.log("tile " + $(this).val() + " clicked");
});
In addition, you can note that $(this)[0] === this
this
should can be used when using built in JavaScript methods, such as .addEventListener()
. Using this
in this context will return the Node that triggered the event.