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... Quote Link to comment 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; } Quote Link to comment Share on other sites More sharing options...
RIRedinPA Posted June 2, 2010 Author Share Posted June 2, 2010 +1! You're my new BFF! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.