javascript - change link text of dynamically appearing links jquery? -
i have dynamic html of link. link appearing every content coming database. functionality same facebook link. when user clicks link text should change 'liked'. link dealing class 'likelink'. here html
while($fetch = mysql_fetch_array($selectresult)) { ?> <div class="col-md-8 col-md-offset-2 well" style="background: #c8c8c8 url('profilepic/b10.jpg'); "> <div class="col-md-2 thumbnail"> <img src="profilepic\<?php echo $fetch['profilepic']; ?>" alt="<?php echo $fetch['profilepic']; ?>"> </div> <div class="col-md-3"> <p><a href="#"><b style="color:darkred;"><?php echo $fetch['name']; ?></b></a></p> <span><?php echo $fetch['postcontent']; ?></span></br> <a href="#" class="likelink" postattrval="<?php echo $fetch['postid'];?>"><i class="fa fa-thumbs-o-up">like</i></a> <span ><?php echo $fetch['visibilty']; ?></span> <span ><?php echo $fetch['createdate']; ?></span> </div> </div> <?php } my jquery code is:
$(document).ready(function(){ $('body').on('click','.likelink', function(){ $.ajax({ type:"post", url:"likescript.php", datatype:"json", data:{ "postid" : $(this).attr('postattrval') }, success:function(data){ if(data == true){ $(this).html('<i class="fa fa-thumbs-o-up">liked</i>'); } else{ alert('something went wrong'); } }, error: function(data){ console.log(data); } }); }); }); any idea how can achieved?
you need store reference of this in variable, in success callback method this doesn't refer element invoked event.
$('body').on('click','.likelink', function(){ //store in variable var _this = $(this); $.ajax({ success:function(data){ if(data == true){ //use stored variable _this.html('<i class="fa fa-thumbs-o-up">liked</i>'); } } }); });
Comments
Post a Comment