Jquery - event does not work on the newly created element
I have a simple comment section where users can post comments to another users post - It works pretty much in the style of facebook wall where a user can write something on the wall and other can comment on it.
So I have this form where the user can enter anything and when he submits - that information is inserted in the database and shown on the same page below the form using Jquery.
Now each of these post have a comment link next to it. So when someone clicks on the comment link - a small textarea box appears where the user can type something and submit.
Everything works great on the previously posted items- except that the comment link on the newly created element does not open up the textbox area.
After searching I came across the Livequery plugin which I implemented - but that doesn't seem to work for me - looks like I'm doing something wrong.
This is the code that I had previously:
$(".comment_button").click(function(){
var element = $(this);
var I = element.attr("id");
//alert("in="+I);;
$("#slidepanel"+I).slideToggle(300);
$(this).toggleClass("active");
return false;
});
I changed this 开发者_运维知识库to use livequery:
$('.comment_button').livequery('click',function(event) {
var element = $(this);
var I = element.attr("id");
//alert("in="+I);;
$("#slidepanel"+I).slideToggle(300);
$(this).toggleClass("active");
return false;
});
This is the link for comment
<a id="<?php echo $data['shopping_id']?>" class="comment_button" href="<?php echo $data['shopping_id']?>">Comment</a>
Thanks for your tips
You don't need livequery
, just live
. Replace livequery
with live
and it should work fine.
精彩评论