Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all elements in a page

Tags:

javascript

I know of two ways to find all the elements in the page.

document.getElementsByTagName('*') and document.all

Is there a better way or is one of these the best?

I'm making an element selector (mostly for practice) and I want to know the most reliable way to get all of the elements in the page, to test against the selector.

like image 596
McKayla Avatar asked Dec 10 '22 10:12

McKayla


1 Answers

document.all is an outdated proprietary method from Microsoft. Don't use it.

document.getElementsByTagName('*') is the W3C standard method of finding all the elements ina document - and surely the fastest - and works in Internet Explorer as well.

P.S. As someone is bound to chime in and provide a jQuery answer to this question, there's how to select all elements in a page using jQuery: jQuery('*');

:-)

like image 197
Már Örlygsson Avatar answered Dec 28 '22 00:12

Már Örlygsson