I’m new in jquery and I suffer from a problem which is a little strange.
After creating a div in jquery,I can’t use it,this example will show you how is the problem!
let’s assume that in the get.php page there is just:echo “test test”;
the code in my file :
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#bda").click(function()
{
$.post("get.php",function(data){
$("#tdiv").append('<div class="info" >'+data +'</div>');
});
});
$(".info").click(function()
{
alert("done");
});
});
</script>
</head>
<body>
<div id="bda">click here</div>
<div align="center" style="border:1px solid blue" id="tdiv">
example
</div>
<div class="info" >it works here</div>
Can some one help me..
What jQuery does with
$(".info")is selecting the.infoelements at the time you call it – which is only one element since the other one is appended later (through ajax). That other element therefore has no click event attached. So, the.infoselector is not actually saved – merely the elements are pulled out of the document and then the selector is discarded.What you want is that jQuery checks element against a selector when you click. You can do that with event delegation: