Dave's Brain

Browse - Programming Tips - Should I use $('body') or $(document.body) ?

Date: 2016aug5
Language: javaScript
Library: jQuery

Q.  Should I use $('body') or $(document.body) ?

A.  Use $(document.body)

'body' is a query to find all tags in the document named <body>.
While with document.body you are handing jQuery the answer.
So you'd expect it to be faster -- even if its a few more characters --
And it is.

Somebody did the tests:

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.