Get all elements of a page or under a tag

Contenu du snippet

Here is a code that retrieve all the tags (under a tag or all in the page).
Can be usefull when you try to reach all elements under a certain div.

Source / Exemple :


/*
getAllElements.js
Author: Benjamin
Version: 1.1
Creation: Unknow
LastModification: 22.02.2011
Copyright: Free of use if you mention the author and source

Version 1.0
=> Retrieve all tags element in the page.

  • /
/* function getNodes(beginNode,bool) Return: nothing Atrributes: beginNode: the tag name to start from; Method: get the children of the current tag; loop the childs when child is a tag do some work if the child as children recall the function with the tag for beginNode
  • /
function getNodes(beginNode){ var nodes = beginNode; nodes = beginNode ? beginNode.childNodes : document.childNodes; for(var i=0; i < nodes.length ; i++){ if(nodes[i].nodeType==1){ /* Do something here In the exemple give the name of the tag in alert mode
  • /
alert(nodes[i].nodeName); if(nodes[i].childNodes){ getNodes(nodes[i]); } } } } //get all elements in the page includes head tags getNodes(); //get only the element under the specified tag var nodes = document.getElementsByTagName('div')[0]; //here the first div childs getNodes(nodes);

Conclusion :


I hope this code will help some people like he helped me.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.