Search code examples
jqueryhtmlreferencedocument-root

Best way to reference root html element with jQuery?


Which is the best way (performance-wise) to get the root document node (the <html> element) in jQuery? I can think of several methods that may or may not work:

$("html")

$(document.documentElement)

$(document) (?)

$.root (?)

$.document (?)


Solution

  • $(document.documentElement) is the fastest, by quite some margin (see tests here).

    You can get more insight as to why this is the case by looking at the jQuery source code (look at the init function, in particular, the part that handles a DOM element, and the part that handles a string).