I have a JS file with a lot of functions that work with DOM manipulation. I added this hover event listener that was from W3 school and getting a 'Cannot read property 'addEventListener' of null' error.
let hover = document.querySelectorAll(".center-nav a");
hover.addEventListener("mouseenter", function( event ) {
// highlight the mouseenter target
event.target.style.color = "purple";
// reset the color after a short delay
setTimeout(function() {
event.target.style.color = "";
}, 500);
}, false);
Did some digging and read it's due to this event is loaded before the pages HTML. Which makes sense because the main.js file is leading in the head.
My functions.php
function mikes_files(){
wp_enqueue_style('main-style', get_stylesheet_uri(), NULL, microtime() );
wp_enqueue_script( 'prefix-font-awesome', 'https://kit.fontawesome.com/xxxxx.js' );
wp_enqueue_script('main-js', get_template_directory_uri() . '/main.js', NULL, microtime() );
}
add_action('wp_enqueue_scripts', 'mikes_files', 999);
Read that the 999 helps with make it load last, but its still in the head.
Do I make a different js file with the code and find the function needed that puts it in the footer?