RIRedinPA Posted June 2, 2010 Share Posted June 2, 2010 Is there a way to make an array of all elements on a page? I tried this //hide initial elements var bodytag = document.getElementsByTagName("body"); var bodyelements = bodytag[0].children for (var e=0; e<bodyelements.length; e++) { alert(bodyelements[e].tagName); } but this only returns the top child element of body (in this case a div). I'm thinking I'll need to use some recursive check to see if the element returned has any children and push them into an array until I have them all... Link to comment https://forums.phpfreaks.com/topic/203648-get-all-elements-on-a-page/ Share on other sites More sharing options...
F1Fan Posted June 2, 2010 Share Posted June 2, 2010 Here's a function that finds all the elements with a certain class name. You should be able to easily modify it to just get every element. function getElementsByClassName(classname, node) { if (!node){ node = document.getElementsByTagName("body")[0]; } var a = []; var re = new RegExp('\\b' + classname + '\\b'); var els = node.getElementsByTagName("*"); for (var i=0,j=els.length; i<j; i++){ if(re.test(els[i].className)){ a.push(els[i]); } } return a; } Link to comment https://forums.phpfreaks.com/topic/203648-get-all-elements-on-a-page/#findComment-1066704 Share on other sites More sharing options...
RIRedinPA Posted June 2, 2010 Author Share Posted June 2, 2010 +1! You're my new BFF! Link to comment https://forums.phpfreaks.com/topic/203648-get-all-elements-on-a-page/#findComment-1066707 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.