Could any one explain me, why does a click event is not triggered, when an element is inserted in the dom from a variable
Consider
HTML
<div id="disp"></div>
<input type="button" value="clickme" id="cme"/>
jQuery
$("#cme").click(function(){
var inside = '<input type="button" value="clickme again" id="sme"/>';
$("#disp").html(inside);
});
$("#sme").click(function(){
alert("clicked me");
});
When you click clickme button, new button clickme again is added inside the disp and when clickme again button is clicked, it does not fire the click event attached to it. it does not alert clicked me ? why ?
But when I have tried this way, it works
$("#cme").click(function(){
var inside = '<input type="button" value="clickme again" id="sme"/>';
$("#disp").html(inside);
// when i put here , it works
$("#sme").click(function(){
alert("clicked me");
});
});
I want to put the $("#sme").click outside. Any help is greatly appreciated. Thanks