I'm trying to toggle between two functions when a "Like" button is clicked.
<div class="like">
<div class="count"><%= post.num_likes %> </div>
<div class="icon" id="like"><i class="icon-thumbs-up-alt"></i></div>
</div>
right now I have:
$(".like").click(function(event){
event.stopPropagation();
$("i", this).toggleClass("icon-thumbs-up-alt").toggleClass("icon icon-thumbs-up");
likePost(TestCanvas, $(this).prev('div').find('.hideThis').text());
});
likePost(canvasID, postID) takes parameters and interacts with an API When I click on .like again, I would like to call unlikePost().
Is there an easy way to toggle between likePost() and unlikePost() when .like is clicked?
Ive tried:
$('.like').toggle(function() {
alert('First handler for .toggle() called.');
likePost(TestCanvas, $(this).prev('div').find('.hideThis').text());
}, function() {
alert('Second handler for .toggle() called.');
unlikePost(TestCanvas, $(this).prev('div').find('.hideThis').text());
});
Its not working as i thought it would though. Seems to execute on page load instead of on click of .like.