1

Im New to Jquery and Js. I need Help Here. I would like to display Images Onclick Instead Of Text

echo '<a href="#" class="like" id="'.$id_post.'" title="Unlike"><img  src="images/fav1.png" width="32"height="32"></a>';
             }else {
       echo '<a href="#" class="like" id="'.$id_post.'" title="Like"><img  src="images/fav.jpg"width="32"height="32"></a>';
   }

jquery

$(document).ready(function(){
          $(document).on('click', '.like', function(){
              if($(this).attr('title') == 'Like'){
                  $that = $(this);
                  $.post('action.php', {pid:$(this).attr('id'), action:'like'},function(){
                      $that.text('Unlike');
                      $that.attr('title','Unlike');
                  });
              }else{
                  if($(this).attr('title') == 'Unlike'){
                      $that = $(this);
                      $.post('action.php', {pid:$(this).attr('id'), action:'unlike'},function(){
                          $that.text('Like');
                          $that.attr('title','Like');
                      });
                  }
              }
          });
      });

please Help

Kiwagi
  • 168
  • 5
  • 17
  • Possible duplicate of [Changing the image source using jQuery](http://stackoverflow.com/questions/554273/changing-the-image-source-using-jquery) – ACOMIT001 Apr 15 '17 at 14:03

1 Answers1

1

Just change the img src on click just the way you did with text.

if($(this).attr('title') == 'Like') {
  $that.find("img").attr("src","images/fav1.png");
}
else if ($(this).attr('title') == 'Like') {
  $that.find("img").attr("src","images/fav.jpg");
}
Dan Philip Bejoy
  • 4,321
  • 1
  • 18
  • 33