Jump to content

get all elements on a page...


RIRedinPA

Recommended Posts

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

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.