Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  48] [ 4]  / answers: 1 / hits: 15895  / 8 Years ago, fri, december 9, 2016, 12:00:00

I want to remove all elements with class sample.



This is working well in Chrome and Safari:



document.querySelectorAll('.sample').forEach(function(e) {
e.parentNode.removeChild(e);
});


Here is the error I get in Firefox:




TypeError: document.querySelectorAll(...).forEach is not a function



More From » foreach

 Answers
7

document.querySelectorAll returns a NodeList which is indexed like an array, but not an Array so you can't call the array methods on it.



You can use Array.from(nodeList) in ES6 or Array.prototype.slice.call(nodeList) for ES5



 Array.from(document.querySelectorAll('selector')).forEach(el => el)

[#59774] Tuesday, December 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;