jquery - Trigger on dynamically created element -


i creating dynamic elements , upon click of button, click event on dynamically generated element need executed. know on() jquery method has such functionality not know how apply in context. glad if help.

html code:

<h2></h2> <button>generate new element</button> <p class="generateevent">event</p> 

my jquery code:

$(function(){      $("button").click(function() {         $("h2").html("<p class='test'>click me</p>")     });      $('.generateevent').click(function(){         $("p").trigger("click", function(){             alert('you clicked me again!');         });     }); }); // method two:  

http://jsfiddle.net/pzsym/399/

you can use on() binding event target's parent container on page when first load page. way:

$('#parent').on('click','.target',function(){    //do something.. }); 

Comments