0
When executing the following code:
$("button").on("click", function(){
console.log("Meu valor é: " + $(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button value="1">Clique-me</button>
The value of the button is returned normally. But in:
$("button").on("click", ()=>{
console.log("Meu valor é " +$(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button value="1">Clique-me</button>
Using Arrow Function in event accuses error in jQuery:
Uncaught Typeerror: Cannot read Property 'toLowerCase' of Undefined
You see I’m using the latest version 3.3.1
jQuery.
Would anyone know why the Arrow Function does not work in cases of jQuery events since ES6 is not something so new (also called Ecmascript 2015) released in June 2015 (almost 3 years!)?
Directly there is no way. But you can make use of the
event.currentTarget
which is the target of the event. See this question on Soen– Isac