Ask coding questions

← Back to all posts
How to make <button> inside <a> not follow the link src?
GavHern (81)

I'm making a friend's website and I have an <a> tag to link to a page about a game he's making. I also have a <button> inside that <a> tag to download the game. Since the button is inside the <a>, it follows the source of the link as well as downloading the file. How can I make the button do its own thing without following the <a> tag? Here's an image of what it looks like.

Answered by JackLee7 (6) [earned 5 cycles]
View Answer
JackLee7 (6)

Add a event listener for the button and use event.stopPropogation(). Let me know if you need help wth this!

GavHern (81)

@JackLee7 yeah i might need help with that...

JackLee7 (6)

@GavHern alright so lets say that your button has the id "download-button"

const button = document.getElementById("download-button");
button.addEventListener("click", e => {
GavHern (81)

@JackLee7 thank you so much! I'll try it out!