I am working with a Javascript code written by someone else who used ES6 in a Wordpress site. It makes an Ajax call to show a data in DOM, which works for Chrome and Firefox but for some reason Safari gives following error in console:
TypeError: document.querySelectorAll(".js_zip-lookup__submit").forEach is not a function. (In 'document.querySelectorAll(".js_zip-lookup__submit").forEach(function(e){e.addEventListener("click",function(){displayResults(e.parentNode.querySelector(".zip-lookup__input").value)})})', 'document.querySelectorAll(".js_zip-lookup__submit").forEach' is undefined)
This is the function:
function finderInit(){
document.querySelectorAll('.js_zip-lookup__submit').forEach(function(button){
button.addEventListener('click', function(){
const zip = button.parentNode.querySelector('.zip-lookup__input').value;
displayResults(zip);
});
});
document.querySelectorAll('.zip-lookup').forEach(function(form){
form.addEventListener('submit', function(e){
e.preventDefault();
const zip = form.querySelector('.zip-lookup__input').value;
displayResults(zip);
})
});
}
And I can't quite tell why Safari would have an issue with this, while Chrome/FF doesn't even log any error about this particular part in the console and it works just fine. I know it should be some browser compatibility issue but haven't found much information so far.
I tried many variations of Array.prototype, but the only thing that solved IE & Safari compatibility issue was inclusion of this polypill snippet below, solution found in this blog:
(function () {
if ( typeof NodeList.prototype.forEach === "function" ) return false;
NodeList.prototype.forEach = Array.prototype.forEach;
})();