243

Using $('html').html() I can get the HTML within the <html> tag (<head>, <body>, etc.). But how can I get the actual HTML of the <html> tag (with attributes)?

Alternatively, is it possible to get the entire HTML of the page (including doctype, <html>, etc.) with jQuery (or plain old JavaScript)?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Justin Stayton
  • 6,031
  • 8
  • 37
  • 43

6 Answers6

466

The simplest way to get the html element natively is:

document.documentElement

Here's the reference: https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.

UPDATE: To then grab the html element as a string you would do:

document.documentElement.outerHTML
Michael
  • 6,895
  • 3
  • 23
  • 19
  • 1
    See also this question for more info on `documentElement` browser compatibility: http://stackoverflow.com/q/11391827/177710. – Oliver Apr 23 '14 at 20:46
  • How often are you getting ref to the html element!? I don't think performance should be a concern, generally. Anyway, this is actually the better answer, but it came in WAY after the award. – posit labs Apr 21 '15 at 01:08
  • 2
    nice, I implemented a brightness slider using this: `` – Khaled.K Oct 13 '21 at 09:25
55

This is how to get the html DOM element purely with JS:

var htmlElement = document.getElementsByTagName("html")[0];

or

var htmlElement = document.querySelector("html");

And if you want to use jQuery to get attributes from it...

$(htmlElement).attr(INSERT-ATTRIBUTE-NAME);
posit labs
  • 8,951
  • 4
  • 36
  • 66
  • Your answer would be considerably better if you could explain _why_ this answered the question. Also, please highlight code and click the `{}` button or press ctrl + k to mark it up as code. – Ben Jan 29 '13 at 22:31
27

In addition to some of the other answers, you could also access the HTML element via:

var htmlEl = document.body.parentNode;

Then you could get the inner HTML content:

var inner = htmlEl.innerHTML;

Doing so this way seems to be marginally faster. If you are just obtaining the HTML element, however, document.body.parentNode seems to be quite a bit faster.

After you have the HTML element, you can mess with the attributes with the getAttribute and setAttribute methods.

For the DOCTYPE, you could use document.doctype, which was elaborated upon in this question.

Community
  • 1
  • 1
doubleswirve
  • 1,388
  • 3
  • 16
  • 23
23

In jQuery:

var html_string = $('html').outerHTML()

In plain Javascript:

var html_string = document.documentElement.outerHTML
berkeleybross
  • 1,314
  • 2
  • 13
  • 27
4

if you want to get an attribute of an HTML element with jQuery you can use .attr();

so $('html').attr('someAttribute'); will give you the value of someAttribute of the element html

http://api.jquery.com/attr/

Additionally:

there is a jQuery plugin here: http://plugins.jquery.com/project/getAttributes

that allows you to get all attributes from an HTML element

Community
  • 1
  • 1
bimbom22
  • 4,510
  • 2
  • 31
  • 46
3

I found other alternative simple way using plain javascript:

const htmlElement = document.querySelector(':root')
console.log(htmlElement)