I have a number of elements in my page that I give the class "page-link" and the attribute "dest='#some-id'".
Then I have this javascript snippet:
window.onload = function() {
setPageLinks();
}
function setPageLinks() {
const PAGE_LINKS = document.getElementsByClassName("page-link");
for (let i = 0; i < PAGE_LINKS.length; i++) {
PAGE_LINKS[i].addEventListener("click", function(event) {
let linkTarget = event.target.getAttribute("dest");
//alert(linkTarget);
$('html, body').animate({
scrollTop: $(linkTarget).offset().top
}, 600);
});
}
}
It makes all the elements with class "page-link" scroll to the element with the ID specified in the "dest" attribute.
It's working fine with all my "page-link" elements, except for one that happens to be a button element. The getAttribute method on that one occasionally - and seemingly randomly - returns null when clicked. This doesn't happen in Firefox, but it happens in Chrome, Edge, and Opera.
This is my button element:
<button type="button" class="page-link" dest="#about">
<span class="front-btn-text">View my work</span>
<span class="front-btn-arrow"><i class="fas fa-arrow-right"></i></span>
</button>
Can anyone tell me why this error occurs, what to do about it (and possibly why it doesn't occur in Firefox?
Thanks!