First put the code you want to run in it's own function, give the anchor tag an id, then add an event listener to detect when the link is clicked and point it at the function
It's an event handler function so the function passes through an event object with information and functions related to the event which is stored in e, you use that to prevent the default action of clicking the link, which would be to go to the anchor or another page.
I think it is better to change your HTML element from a link to a button. I think some browsers may check your <a> link underground so the event maybe automatically triggers. There may be some other issues, but I am not sure.
Bookmarks