I wrote the following function and put it between script tags in the header of my wordpress website.
function turnShopBlue(){
var location = window.location.href;
if(location === "MY URL GOES HERE"){
var menuItem = document.getElementById("menu-item-3352");
menuItem.classList.add("current-menu-item");
//document.querySelector("#menu-item-3352").addClass("current-menu-item");
}
}
turnShopBlue();
But for some reason the function doesn't run. Each time I go the URL (page) that I choose in "MY URL GOES HERE", the class current-menu-item doesn't get added. What do I miss?
I've already tried with triple === and double ==, without success.
EDIT SOLUTION:
My mistake was that I placed the script in the header, the problem here is that the script then runs before the entire DOM is rendered. So alot of the variables I instantiate don't have the good values. The problem was solved by loading the script in the header;
Also, I had to put it in a jQuery document ready function to autoload it on pageload.