Search code examples
javascripthtmlcsscss-selectorsprototypejs

How do i get all children (ie children of children) with Javascript?


I need a javascript function or a CSS Selector (or even a ProtoypeJS function) which gets me ALL elements (ie. descendants) under a particular element.

I understand there are CSS selectors such as : 'p, a, div', which would get me all the elements of those three types. However I need to get EVERYTHING without specifying type.

ie. I want something like

myElement.getElements('*')

Solution

  • Compatible with all versions of IE, not just IE8+...

    myElement.getElementsByTagName("*")
    

    * is treated as a special case for getElementsByTagName to return all descendants regardless of tag name. (This is different to querySelectorAll, where * is a genuine selector that matches all elements)