Here's the sample code you've asked for. There are two link elements with different href attributes. When clicked, an ajax request will be fired to whatever is specified as the href and on success the respective span will be filled with a string and be shown for 3 seconds.
<a onclick='return eachlike(this)' href="https://www.jamarkoho.com/liked/1" class="btn btn-outline-dark btn-sm">
<i class="far fa-thumbs-up me-1"></i>
Like <span class="here"></span>
</a>
<a onclick='return eachlike(this)' href="https://www.jamarkoho.com/liked/2" class="btn btn-outline-dark btn-sm">
<i class="far fa-thumbs-up me-1"></i>
Like <span class="here"></span>
</a>
<script>
function eachlike(thislike) {
$.ajax({
type: 'GET',
url: $(thislike).attr('href'),
success: function(response) {
$(thislike).find('.here').html('Done!').show().delay(3000).fadeOut();
}
});
return false;
}
</script>
To actually toggle the href (like / unlike), use something like this:
<a onclick='return eachlike(this)' href="https://www.jamarkoho.com/liked/1" data-toggle-href="https://www.jamarkoho.com/unlike/1" class="btn btn-outline-dark btn-sm">
<i class="far fa-thumbs-up me-1"></i>
Like <span class="here"></span>
</a>
<a onclick='return eachlike(this)' href="https://www.jamarkoho.com/liked/2" data-toggle-href="https://www.jamarkoho.com/unlike/2" class="btn btn-outline-dark btn-sm">
<i class="far fa-thumbs-up me-1"></i>
Like <span class="here"></span>
</a>
<script>
function eachlike(thislike) {
$.ajax({
type: 'GET',
url: $(thislike).attr('href'),
success: function(response) {
$(thislike).find('.here').html('Done!').show().delay(3000).fadeOut();
toggleHref(thislike);
}
});
return false;
}
function toggleHref(thislike) {
let currentHref = $(thislike).attr('href');
let newHref = $(thislike).data('toggle-href');
$(thislike).attr('href', newHref);
$(thislike).data('toggle-href', currentHref);
}
</script>
The second function toggles between href and new attribute data-toggle-href.
$profile->profile_idcoming from? You also need to return false to theonclickimmediately, not from the callback.thisLikewill be a string, not a reference to aformelement, so callingserialize()on that isn't going to work.attr('href'). You probably meant to putonclick='eachlike(this)'