I'm trying to take the function outside the loop and then call it from within, but I'm not sure how to do it.
const links = document.querySelectorAll( 'a' );
for ( let i = 0; i < links.length; i++ ) {
links[i].addEventListener( 'click', ( event ) => {
const targetID = '#' === event.currentTarget.getAttribute( 'href' ) ? 'start' : event.currentTarget.getAttribute( 'href' );
...rest of the function...
} );
}
This is what I've tried so far:
const links = document.querySelectorAll( 'a' );
function smoothScrolling( event ) {
const targetID = '#' === event.currentTarget.getAttribute( 'href' ) ? 'start' : event.currentTarget.getAttribute( 'href' );
...rest of the function...
}
for ( let i = 0; i < links.length; i++ ) {
links[i].addEventListener( 'click', smoothScrolling( event ) );
}
I'm not sure why, but I'm getting the following error: Uncaught TypeError: Cannot read property 'currentTarget' of undefined.
links[i].addEventListener( 'click', smoothScrolling);func()is calling the function.funcis referencing/passing the function.