I'm really confuse about how to add a click event to element created by jquery.
Now, I generate elements like this:
$.ajax(
params....
)
.done(function(item){
$.each(item, function(i, some){
a = '<p>some text</p>';
$(node).before(a);
}
});
my problem is when I try to add a click event to element "p". If I do:
$('p').on('click', 'p', function(){
alert('yai!');
});
Is showing nothing. BUt if I do:
$.ajax(
params....
)
.done(function(item){
$.each(item, function(i, some){
a = '<p>some text</p>';
$(a).click(function(){
alert('yai!');
});
$(node).before(a);
}
});
It show too many alerts (the same number of p elements)
What I'm doing wrong?