I am really confused how to add a click event to an element created by jquery.
Now I am generating the following elements:
$.ajax( params.... ) .done(function(item){ $.each(item, function(i, some){ a = '<p>some text</p>'; $(node).before(a); } });
My problem is that I am trying to add a click event to the "p" element. If I do this:
$('p').on('click', 'p', function(){ alert('yai!'); });
It doesnβt show anything yet. BUt if I do this:
$.ajax( params.... ) .done(function(item){ $.each(item, function(i, some){ a = '<p>some text</p>'; $(a).click(function(){ alert('yai!'); }); $(node).before(a); } });
It displays too many warnings (the same number of p elements)
What am I doing wrong?
source share