Is there a way to retrieve the ID of an element when it is clicked, only if it has one?
I currently have this code set up to alert me with the element's ID:
$("[id]").click(function(event) {
event.preventDefault();
var id_name = $(this).attr("id");
alert(id_name);
});
However, I am looking to get the ID of the topmost element only.
For example, let's say I have a button inside a div, both with their own ID attributes. If I click on the button, I only want to receive the ID of the button itself and not the div. Currently, my script alerts me of both IDs. How can I modify my code to achieve getting only the ID of the topmost element?