I want to get the ID of an element that I click. It should only give me the ID if it has one.
I have this code to alert me the element ID:
$("[id]").click(function(event) {
event.preventDefault();
var id_name = $(this).attr("id");
alert(id_name);
});
But I only want the foremost element's ID.
I have a button inside a div, both with an id-attribute. If I click the button, I only want the ID of the button. However, my script alerts me of both the buttons ID and the div's ID. Why is that/How can I get only the foremost element's ID?