yuvi yuvi - 7 months ago 13
PHP Question

rel attributte not working in this code

From this code i wrote in select query it will work fine,in database i have 10 number rows so it will come 10 comments,for my problem is,i have one reply button while click the reply button i want get id,from here i got only first row id,i am clicking all button i got id=1,i thing rel attribute it will make problem but i don't know exatly y? it is not working



<?php
$comment = mysql_query("SELECT * FROM task_comments WHERE task_id='$task_id'");
while($com=mysql_fetch_assoc($comment)){
?>
<div class="comment">
<div class="img-thumbnail">
<img class="avatar" alt="" src="../TV/dist/img/user2-160x160.jpg">
</div>
<div class="comment-block">
<div class="comment-arrow"></div>
<span class="comment-by">
<strong><?php echo $com['comment_by'];?></strong>
<span class="pull-right">
<a href="#" rel="<?php echo $com['id']?>" class="reply-btn"><i class="fa fa-reply"></i> Reply</a>
</span>
</span>
<p><?php echo $com['comment'];?></p>
<span class="date pull-right"><?php echo $com['comment_on'];?></span>
</div>
</div>
<?php } ?>

<!-- My JS code Like this -->

$(document).ready(function(){
$('.reply-btn').click(function(event){
event.preventDefault();
alert($(".reply-btn").attr("rel"));// here i got only first row id name like 1
});
});




Answer

try $(this) to get the data from the clicked button. I would personally recoemmend using data attributes though. and probably suggest removing the spans from around the link anyway.

 $(document).ready(function(){
     $('.reply-btn').click(function(event){
        alert($(this).attr("rel"));// should give the the "rel" of the clicked button
        });
     });
Comments