I have a table with multiple rows, and on each row, there is an edit
and delete
button.
In short, when an edit button is triggered with class .edit
, a form pops-up. Along with the class name, I have also included a unique id
like id="edit-32"
, where the number changes depending on what row the user clicks on.
The problem I am trying to solve is to capture the id attributes number after the dash. Previously, in ES5, I have simply used this
keywoard to target the current element being clicked. But in ES6, this
keywoard is refereing to the class environment.
ES5 approach (returns unique ID)
// Open edit form
$('.edit').click(function(){
// Return id attribute
const id = $(this).attr('id');
// print to console
console.log(id);
});
ES6 approach (Returns undefined)
// Open edit form
$('.edit').click(() => {
// Return id attribute
const id = $(this).attr('id');
// print to console
console.log(id);
});
Is there a way I can make arrow functions work with similar approach to ES5?