Reputation: 29
I created an jQuery function that generates links and write it in an div container in html
The Result is:
<div id="breadcrumbs">
<a id="crumb1">Crumb1</a>
<a id="crumb2">Crumb2</a>
</div>
Yet I want to get an alert if some of the dynamically created links was clicked. In this alert I want to show the clicked ID. How can i do this? Can i bind a click() event to the div-Tag which alerts me the clicked ID of the -Tag?
Thx for help me
Upvotes: 0
Views: 323
Reputation: 388446
You need to use a event delegation based click handler since the target elements are created dynamically.
jQuery(function () {
$('#breadcrumbs').on('click', 'a', function () {
alert(this.id)
})
})
Inside the click handler this
will refer to the targeted element(in this case the anchor element), so you can get the id
using this.id
Upvotes: 3
Reputation: 150080
Put the following either in a script element at the end of the document body or in a document ready handler:
$("#breadcrumbs").on("click", "a", function(e) {
alert(this.id);
});
Upvotes: 1