This is my HTML:
<div action="https://github.com/">
<div class="menu-bar-item">
<h1 class="menu-item-text">GitHub</h1>
</div>
</div>
And JavaScript:
$(document).on("click", "*[action]", (e) => {
try {
var attribute = $(e.target).attr("action");
alert(attribute)
} catch(e){}
})
Here attribute action is sometimes retuning "undefined". How do I fix?
Get the event's currentTarget instead, which returns the element the event listener has been attached to:
$(document).on("click", "*[action]", (e) => {
try {
var attribute = $(e.currentTarget).attr("action");
alert(attribute)
} catch (e) {}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div action="https://github.com/">
<div class="menu-bar-item">
<h1 class="menu-item-text">GitHub</h1>
</div>
</div>