The JS DOM below is for modifying through classes, the styles of two elements that are dynamically created in the document. But it is not always that these elements will appear in the document. Maybe they show up, maybe not. When they are, the code works fine, and when they are not, the console returns TypeError: jsNavButton is undefined
. How do I prevent this?
var jsNavButton = document.querySelectorAll(".js-nav-toggle")[0],
jsNavHidden = document.querySelectorAll(".js-nav-hidden")[0];
jsNavButton.style.backgroundColor = "tomato";
jsNavHidden.style.backgroundColor = "blue";