Reputation: 4963
I am trying to register click
event on anchor
tag created dynamically but it is not working. Following is code
var location = 's';
$('#phone').on('click', 'a', function(event) {
console.info('Anchor clicked!');
event.preventDefault();
return false;
});
for(var i = 0; i < 5; i++) {
$("div#phone").append('<ul><a href="#" id = "' + location + '"> rajeev </a></ul>');
}
Have a look at Fiddle
Edit Updated Fiddle Link
Upvotes: 0
Views: 177
Reputation: 10724
Works fine if you place your code after your loop.
https://jsfiddle.net/3oy472om/
for(var i = 0; i < 5; i++) {
$("div#phone").append('<ul><a href="#" id = "' + location + '"> rajeev </a></ul>');
}
$('#phone a').on('click', function(event) {
alert('Anchor clicked!');
event.preventDefault();
return false;
});
Upvotes: 0
Reputation: 4572
Your code works well, check your browser's console or change the console for alert:
$('#phone').on('click', 'a', function(event) {
alert('Anchor clicked!');
event.preventDefault();
return false;
});
Upvotes: 1