I am trying to create an if statement where the script checks if an element exists, hides it, and if it has hidden it, a dataLayer event is sent. If not, another event is sent.
However, I cannot seem to get a basic if statement functioning. What could be wrong with my code?
Only vanilla JS is possible.
function hideElement() {
if (element.classList.contains(some-element-class)) {
while (elements.length > 0) elements[0].remove();
// so this is where I would add some dataLayer push event "element shown"
}
else {
console.log(computer says no)
// so this is where I would add some dataLayer push event "element not shown"
}}
This however does work on its own, but I would like to add the event as well:
const elements = document.getElementsByClassName("some-element-class");
while (elements.length > 0) elements[0].remove();