Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Way to Get All DOM Elements with jQuery

What's the best way to get all of the DOM elements on a page using jQuery?

Thanks,

DLiKS

Edit: This is for use in a script that grayscales an entire page using grayscale.js - http://james.padolsey.com/demos/grayscale/. jQuery because I can! :P

like image 947
DLiKS Avatar asked Oct 26 '10 13:10

DLiKS


2 Answers

var allOfThem = $('*');

You don't really need jQuery for this:

var allOfThem = document.getElementsByTagName('*');
like image 150
Pointy Avatar answered Sep 18 '22 17:09

Pointy


document.getElementsByTagName("*") will return all DOM elements as "actual" elements, with all their contents and properties and everything.

$('*') or $("body *") will return array of "jQuery objects", each only pointing on true element. To get the true element, you'll have to use the specific jQuery object.

Guess this difference is what causing this behavior of browser crashing when getting all elements vs. getting all jQuery objects.

like image 40
Shadow Wizard Hates Omicron Avatar answered Sep 22 '22 17:09

Shadow Wizard Hates Omicron