How to get the `` tag HTML with JavaScript / jQuery?
Here's the punchline:
To get the entire <html>
markup:
JavaScript: document.documentElement.outerHTML;
jQuery: $('html')[0].outerHTML;
Detailed walkthrough: Retrieval and manipulation methods
For those who value not just the "how", but the "why" as well, let's dive deep into the ocean of HTML tag manipulation.
Retaining the doctype
A doctype can be as aloof as a cat, but JavaScript's got a tool to tame it:
Digging for HTML tag attributes
To get an attribute from the <html>
tag, you've got options:
JavaScript:
jQuery:
Futzing with <html>
attributes – handle with care!
Changing the attributes? Easy peasy. But remember: With great power comes great responsibility:
JavaScript:
jQuery:
Extras: Alternative methods and insider tricks
Like a Swiss army knife, JavaScript has a tool for every job. Let's explore more of them:
Wizarding ways to access HTML
JavaScript has many spells to summon the html DOM element:
The need for speed: jQuery vs JavaScript
Pure JS is a hare and jQuery is the tortoise when it comes to performance. Tortoise wins the story, but not in coding:
Supercharging with plugins
Enhance your jQuery experience with plugins! Ever heard about getAttributes
? It fetches all attributes of an element:
Was this article helpful?