Skip to content Skip to sidebar Skip to footer

Yet Another Uncaught Typeerror: : Cannot Read Property 'replace' Of Undefined

I am writing a bit of JS code to switch classes for certain DOM elements. Everything is working as it should, but I am also getting this error and this prevents the code that follo

Solution 1:

Never use for...in loops to iterate array-like objects!!!!!

In this case, the HTMLCollection has enumerable properties like item or namedItem. Accessing these won't return an HTML element, so className will be undefined.

To iterate properly, you can use one of these

for(var i=0; i<responsive_elements.length; ++i) {
  // ...
}

[].forEach.call(responsive_elements, function(element, index, collection) {
  // ...
});

Post a Comment for "Yet Another Uncaught Typeerror: : Cannot Read Property 'replace' Of Undefined"