Reputation: 178
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
Upvotes: 1
Views: 3078
Reputation: 4381
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");
}
Upvotes: 1