HTML
<div class="sym-box" data-card="0">
<div id="a"><img src="..."></div>
</div>
<div class="sym-box" data-card="1">
<div id="b"><img src="..."></div>
</div>
<div class="sym-box" data-card="2">
<div id="c"><img src="..."></div>
</div>
JS
var attribute;
function myFunction() {
attribute = this.getAttribute('data-card');
}
var symboxes = document.getElementsByClassName('sym-box');
for (i = 0; i < symboxes.length; i++) {
symboxes[i].addEventListener('click', myFunction, false);
}
The code above works as expected, 'this' references the clicked element. However, when I write it as an arrow function I get "Uncaught TypeError: this.getAttribute is not a function" because 'this' is now referring to the window object.
const myFunction = () => {
attribute = this.getAttribute('data-card');
};
So my question is how do I rewrite myFunction() as an arrow function where 'this' refers to the clicked element?
NOTE: My question has been marked as a duplicate but I am asking for a work around to achieve the same thing when using an arrow function. Just being told "You can't" or "Don't" didn't answer my question.