SO,
My question is fairly simple I guess. I provided a small code snippet to help illustrate the problem.
I'm trying to grab the button's id. It works fine unless you click one any of the text inside the button, then the jQuery tries to grab that instead.
I am using event.target.attributes.id.value
to locate the id attribute of the clicked element, but if anyone knows a simpler way by all means please share this!
If you need help understanding the problem, just run the code snip and attempt clicking on the button, then on the text inside the button. The console log statement should show the id of the button clicked no matter what.
Thank you to anyone who can help.
$(document).on("click", ".eat", event => {
event.preventDefault();
const id = parseInt(event.target.attributes.id.value);
console.log(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" class="btn btn-success eat" id="10">
<p>What is my ID?</p>
<h4>---> Click to find out! ---></h4>
</button>
<button type="button" class="btn btn-success eat" id="11">
<p>What is my ID?</p>
<h4>---> Click to find out! ---></h4>
</button>