Jquery like image changing mechanism is not working -
i created program in php
, jquery
. here jquery
code:
$(document).ready(function(){ $(".likethis").click(function(event){ event.preventdefault(); var id = $(this).attr("href"); $.ajax({ url: '<?php echo site_url().'users/likevideo/'; ?>'+id, success: function(data) { var d =data.split('~'); if(d[0]==2){ alert('you must login like-dislike video!'); }else if(d[0]==3){ alert('you liked video'); }else if(d[0]==1){ $(".like_ico").html(d[1]); $(".dilike_ico").html(d[2]); $("#like_box").html('<a href="#" class="likeedthis"><img src="<?php echo base_url(); ?>images/like.jpg" /></a>'); $("#dislike_box").html('<a href="<?php echo $vid->id; ?>" class="dlikethis"><img src="<?php echo base_url();?>images/dis_lik_over.jpg" /></a>'); } } }); }); $(".dlikethis").click(function(event){ event.preventdefault(); var id = $(this).attr("href"); $.ajax({ url: '<?php echo site_url().'users/dlikevideo/'; ?>'+id, success: function(data) { var d =data.split('~'); if(d[0]==2){ alert('you must login like-dislike video!'); }else if(d[0]==3){ alert('you disliked video'); }else if(d[0]==1){ $(".dilike_ico").html(d[1]); $(".like_ico").html(d[2]); $("#dislike_box").html('<a href="#" class="dlikedthis"><img src="<?php echo base_url(); ?>images/dis_lik.jpg" /></a>'); $("#like_box").html('<a href="<?php echo $vid->id; ?>" class="likethis"><img src="<?php echo base_url();?>images/like_over.jpg" /></a>'); } } }); }); });
and here html.
<div id="dislike_box"> <?php $likeimage=''; $dlikeimage=''; if($this->basic->get_video_liked_or_not($vid->id)==1){ $likeimage ='<a href="#" class="likeedthis"><img src="'.base_url().'images/like.jpg" /></a>'; }else{ $likeimage ='<a href="'.$vid->id.'" class="likethis"><img src="'.base_url().'images/like_over.jpg" /></a>'; } if($this->basic->get_video_dliked_or_not($vid->id)==1){ $dlikeimage='<a href="#" class="dlikedthis"><img src="'.base_url().'images/dis_lik.jpg" /></a>'; }else{ $dlikeimage='<a href="'.$vid->id.'" class="dlikethis"><img src="'.base_url().'images/dis_lik_over.jpg" /></a>'; } ?> <?php echo $dlikeimage; ?> </div> <div id="like_box"> <?php echo $likeimage; ?> </div>
the problem when click button first time image changing 'liked' image, if click dislike button page refreshing instead of disliking video. after refreshing clicked dislike again. working. same happens button. can tell me why happen?
use event delegation as adding elements dynamically
$("#dislike_box").on('click',".dlikethis",function(event){..}); $("#like_box").on('click',".likethis",function(event){..});
Comments
Post a Comment