In my long, multi-page HTML document, I want to include multiple buttons that look the same and do the same thing (location.reload();). With the idea of not repeating code, I would like to make a single onclick event that works for all of them. But I can't figure out how.
I've tried playing with giving them all the same .class of "reloadButton" instead of an id, such as <button class="reloadButton">Reload</button>
. Then I tried to use var reload = documents.getElementsByClassName("reloadButton");
.
But I don't know what to do from there. Trying something like
reload.onclick = function () { location.reload(); }
doesn't seem to work.
I'm not sure how to use a for loop to go through all the array-like HTMLCollection when it's attached to the onclick event.
This is with just pure JavaScript and my level of expertise is pure beginner too. So, I would love it if you might be able to explain it with that in a mind or if you could link a website that explains this at a level I might be able to understand. Thank you!