Every time the like button is clicked, the webpage scrolls to the top. Any ideas how to stop this from happening. The code is ,
<a href="/likeComment/{{../post._id}}/{{_id}}">
<i class="fa-solid fa-thumbs-up"></i>
{{likes.length}} Like
</a>
The like is registered and the number of likes is incremented. However, it jumps to the top of the webpage every time it is clicked. I want the webpage to stay where it is when the like is clicked.
2
Answers
Ok I solved this by adding onclick to the element
then using AJAX to update the like count.
It’s the default behavior of an anchor
<a>
element with anhref
. When you click on it can cause the page to jump to the top.Changing it to:
<button>
or<div>
should resolve the issue.