Difference between $(document.body) and $('body')

user1617640 picture user1617640 · Sep 6, 2012 · Viewed 178.8k times · Source

I am a jQuery beginner and while going through some code examples I found:

$(document.body) and $('body')

Is there any difference between these two?

Answer

Justin Ethier picture Justin Ethier · Sep 6, 2012

They refer to the same element, the difference is that when you say document.body you are passing the element directly to jQuery. Alternatively, when you pass the string 'body', the jQuery selector engine has to interpret the string to figure out what element(s) it refers to.

In practice either will get the job done.

If you are interested, there is more information in the documentation for the jQuery function.