This can be done by using getElementsByTagName() or querySelectorAll(). Old browsers don’t support the querySelectorAll() so in such a case you have to use the other one and then manually write a for loop to iterate over each node and then parse it to get the actual content.
Whereas in the latest browsers we have the querySelectorAll() and with that, we can use foreach to automatically loop through each node directly in the DOM and will get the node object.
Following are the examples with both getElementsByTagName and querySelectorAll()
var domNodes = document.getElementsByTagName("*");
for (var i=0, domLength=all.length; i < domLength; i++) {
console.log(domNodes[i]);
// Parse the node string and do other operations you want.
}
document.querySelectorAll('*').forEach(function(node) {
// use node object to get the details of the node.
});
Leave a Comment