0

I have added an element using jquery.html but I can't seem to run another function on that element?? Any ideas

Fiddle http://jsfiddle.net/0j50L4uo/1/

<a class="trigger">Cick here A</a>
<br/><br/>
<div id="test"></div>

$(".trigger").click(function() {
    alert( "click" );

});

$('#test').html('<a class="trigger">Click here B</a>');
sam
  • 345
  • 3
  • 12

1 Answers1

0

click() function won't listen to dynamically created elements.

Use on() function (event delegation) in order to attach event listeners to dynamically added elements:

Check the DEMO

$(document).on('click', '.trigger', function() { 
  // ... 
});
kapantzak
  • 11,610
  • 4
  • 39
  • 61