Both Visual Studio Code and the Mozilla Web Docs tell me that document.all
is deprecated and should not be used in new websites, like mine.
However, document.all
seemed to serve a very important purpose: getting a list of every node and their children in a document (either for manipulating the current HTML page, or when parsing XML file or string).
I've seen a lot of advice that suggests document.getElementById
instead. But that only solves the problem of using document.all
with indices to fetch one element (which is pretty obviously bad practice.) This in no way offers a solution for simply getting a list of every element and their children in a DOM, especially for iterating.
What is the proper, modern replacement for this method?
Both Visual Studio Code and the Mozilla Web Docs tell me that document.all
is deprecated and should not be used in new websites, like mine.
However, document.all
seemed to serve a very important purpose: getting a list of every node and their children in a document (either for manipulating the current HTML page, or when parsing XML file or string).
I've seen a lot of advice that suggests document.getElementById
instead. But that only solves the problem of using document.all
with indices to fetch one element (which is pretty obviously bad practice.) This in no way offers a solution for simply getting a list of every element and their children in a DOM, especially for iterating.
What is the proper, modern replacement for this method?
document.querySelectorAll("*")
?
– Nicholas Tower
Commented
May 26, 2021 at 16:35
document.querySelectorAll("*")
returns exact same # of elements as document.all
on this page.
– spender
Commented
May 26, 2021 at 16:38
TreeWalker
you might want to look at. But recursive functions are the more natural approach to processing recursive structures imo. If you ask a new question about the actual data processing you want to do, with an example of your xml, we might be able to suggest a more concrete solution
– Bergi
Commented
May 26, 2021 at 17:30
A solution for html would be document.querySelectorAll("*")
What is the proper, modern replacement for this method, especially for iterating?
A NodeIterator
.