0

I have a js function which calls 'generate.php' on click event. The php returns an anchor tag with id 'alert'. the js functions appends this anchor tag to the document. Another js function acts on this dynamically generated anchor. As title says the .on() function doesn't bind the event handler on this anchor tag.Please help me out...

My js file is as below

$("#generate").click(function(){       // Working Fine :)
     $.post("generate.php",function(reply){
           $("reply").html(reply);
     }
});    

$("#alert").on("click",function(){     //Not Working :(
     alert("hello");
}

My html code is as below

<a href="#" id="generate">Click TO generate</a>
<div id = "reply"></div>

My generate.php looks like this

<?php //Working Fine
echo '<a href="#" id="alert">Click To get alert Box</a>'; 
echo '<div id="abc">Click The above link to get an alert</div>';
?>
PrathapB
  • 382
  • 1
  • 4
  • 15

1 Answers1

0

Delegation is use like this. Refer .on()

$(document).on("click","#alert",function(){   
     alert("hello");
}); //added missing );

Tip : You are appending elements with hard-coded ID, this will result in duplicate elements.

Shaunak D
  • 20,588
  • 10
  • 46
  • 79