-2

After appending a div onclick is not working. Please help me.

jQuery(document).ready(function() {
    jQuery(".chack").click(function(){
        jQuery( "#pc_form" ).append('<br><div class="apply">apply</div>');
    });

    jQuery(".apply").click(function(){  
        alert();    
    }); 
});
<div class="pc_form" id="pc_form">  
    <div class="apply">apply here</div>
</div>
<br /><br />
<div class="chack">click</div>  
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Nikhil Vaghela
  • 222
  • 1
  • 14
  • Dynamically inserted element needs delegated event handlers – adeneo Jan 06 '15 at 10:24
  • how can do plz tell me – Nikhil Vaghela Jan 06 '15 at 10:25
  • 1
    To notify adeneo, use the `@username` syntax: "@adeneo" (note that he should be notified by this comment, so there's no need to spam him beyond this point). – David Thomas Jan 06 '15 at 10:27
  • Those `
    `s are not keyboard accessible. Replace them with real `
    – danielnixon Jan 06 '15 at 10:27
  • @DavidThomas - And I was notified, thank you! Nikhil, to learn about delegated event handlers, look at the answer below, the duplicate, and do a search, there are thousands of answers on SO that explain this. Also, jQuery has documentation -> **http://learn.jquery.com/events/event-delegation/** – adeneo Jan 06 '15 at 10:31

3 Answers3

1
$('#pc_form').live("click", ".apply", function() {  alert(); });

live event workig

Nikhil Vaghela
  • 222
  • 1
  • 14
0

You need to use event delegation to attach events to dynamically added elements:

jQuery("body").on('click','.apply',function(){  
   alert('tst');    
}); 
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

The div.apply is added with jquery, you have to bind the event to it.Try with -

jQuery( "#pc_form" ).append('<br><div class="apply">apply</div>').bind('click', function(e) {
   e.stopPropagation();
   alert('hello');
})
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87