The below code works fine, independently. Meaning, if I come to the page and smash the like button it will function correctly and display the unlike button. However, if I immediately click the unlike button following that, nothing will happen. I will have to refresh the page and functionality will resume. Am I missing a crucial step here or is my logic flawed?
$(document).ready(function(){
var content = <?php echo $content['id']; ?>;
user = <?php echo $user['id']; ?>;
$('.like').on('click', function(e){
e.preventDefault();
$.ajax({
url: 'ajax.php',
type: 'post',
data: {
'content': content,
'user': user,
'like': 1
},
success: function(response){
$(".like").replaceWith( "<a href='#' class='btn btn-sm btn-danger rounded-pill unlike' data-id='<?php echo $content['id']; ?>'><i class='fas fa-skull'></i> Boo!</a>" );
}
});
});
$('.unlike').on('click', function(e){
e.preventDefault();
$.ajax({
url: 'ajax.php',
type: 'post',
data: {
'content': content,
'user': user,
'unlike': 1
},
success: function(response){
$(".unlike").replaceWith( "<a href='#' class='btn btn-sm btn-success rounded-pill like' data-id='<?php echo $content['id']; ?>'><i class='fas fa-heart'></i> Woo!</a>" );
}
});
});
});