If I do this in the console:
document.querySelectorAll('.error')
An object of length 3 is returned.
If any of the property values are empty, then I do not want my if statement to run:
After getting frustrated with this (I was using != '') the internet told me to use !== ""
Here is the output for property in the object:
document.querySelectorAll('.error')[0]
""
document.querySelectorAll('.error')[1]
"some string"
document.querySelectorAll('.error')[2]
""
My newness to JS is probably apparent by my question so far. Is the "best" way to check for empty string to use what I have done !==?
<script>
var errorElems = document.querySelectorAll('.error');
for(i=0; i<errorElems.length; i++) {
if(errorElems[i].textContent !== ""){
dataLayer.push({
"event": "errors",
"eventCategory": "error messages",
"eventAction": errorElems[i].id,
"eventLabel": errorElems[i].textContent
})
};
};
This appears to be working. But I'm one person checking in one browser (Chrome) on a desktop machine, if that matters.
What would be the most full proof (yet reasonably straightforwards) means to doing this? Is !== best test?