You can use a DOMParser, like so:

var xmlString = "<div id='foo'><a href='#'>Link</a><span></span></div>";
var doc = new DOMParser().parseFromString(xmlString, "text/xml");
console.log(doc.firstChild.innerHTML); // => <a href="#">Link...
console.log(doc.firstChild.firstChild.innerHTML); // => Link
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from maerics on Stack Overflow
🌐
David Walsh
davidwalsh.name › convert-html-stings-dom-nodes
Convert String to DOM Nodes
March 20, 2017 - The DOMParser API allows developers to create DOM nodes from and HTML string using JavaScript.
🌐
Medium
medium.com › @GemmaBlack › converting-a-string-into-html-dom-nodes-3b04d73e413c
Converting A String Into HTML Dom Nodes | by Gemma Black | Medium
January 25, 2018 - let parser = stringToParse => { let dom = document.createElement('div'); let cleanedString = cleanHTMLString(stringToParse); dom.innerHTML = cleanedString; return dom.firstChild; } If you noticed our little cleanHTMLString function. It must ensure that we only get 1 single Dom node.
🌐
Codingshower
codingshower.com › convert-html-string-to-dom-node
JavaScript Parse and Convert Raw HTML String to DOM Nodes – CodingShower
April 3, 2022 - Effectively the template.content property will end up having the DOM nodes wrapped in a DocumentFragment. const template = document.createElement('template'); template.innerHTML = htmlString; const fragment = template.content; Using the DOMParser.parseFromString() method, we can parse and convert ...
🌐
Java2s
java2s.com › example › nodejs › string › convert-string-to-html-entity.html
Convert String to HTML entity - Node.js String
Node.js · String · HTML String · Convert String to HTML entity · String.prototype.entityify = function () { return this.// www .j ava 2 s . com replace(/&/g, '&amp;'). replace(/</g, '&lt;'). replace(/>/g, '&gt;'); }; Previous · Next · Encode HTML string ·
🌐
Go Make Things
gomakethings.com › converting-a-string-into-markup-with-vanilla-js
Converting a string into markup with vanilla JS | Go Make Things
August 13, 2019 - The simplest way to do this is to create an element, insert the string into with innerHTML, then return the element. /** * Convert a template string into HTML DOM nodes * @param {String} str The template string * @return {Node} The template ...
🌐
Designcise
designcise.com › web › tutorial › how-to-convert-an-html-string-to-javascript-nodelist
How to Convert an HTML String to JavaScript NodeList? - Designcise
October 5, 2020 - Let's suppose we have a string like the following which we want to parse and get the DOM elements as NodeList: const htmlStr = ` <html> <head> <title>hello world</title> <link rel="stylesheet" href="style.css" /> </head> <body> <p>foo</p> <p>bar</p> <a href="#baz">baz</a> </body> </html> `; First thing we need to do is to convert this string into a DOM Document tree object like so: const parser = new DOMParser(); const doc = parser.parseFromString(htmlStr, 'text/html'); console.log(doc); /* Output: #document <html> <head> <title>hello world</title> </head> <body> <p>foo</p> <p>bar</p> <a href=
🌐
Melvin George
melvingeorge.me › blog › convert-html-string-into-real-html-or-dom-javascript
How to convert an HTML string into real HTML or DOM using JavaScript? | MELVIN GEORGE
April 26, 2021 - To convert an HTML string into real HTML or DOM, you can use the DOMParser Web API using JavaScript. The DOMParser helps us to parse HTML or XML string into real Document or DOM nodes.
🌐
npm
npmjs.com › package › node-html-parser
node-html-parser - npm
1 month ago - Insert one or multiple nodes or text to the last position of an element's child nodes. This is similar to appendChild, but accepts arbitrarily many nodes and converts strings to text nodes. Append a node to an element's child nodes. Parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position.
      » npm install node-html-parser
    
Published: Sep 07, 2026
Version: 9.0.4
Find elsewhere
🌐
npm
npmjs.com › package › string-to-html
string-to-html - npm
May 8, 2017 - For when you want to use vanilla JS and parse html strings into a DOM fragments.
      » npm install string-to-html
    
Published: May 08, 2017
Version: 1.3.4
🌐
Norday Tech
norday.tech › posts › create-dom-node-from-html-string
Create a DOM node from an HTML string | Norday Tech
June 14, 2019 - Here are the results of multiple runs of a jsPerf benchmark, which ran in the latest versions of Chrome and Firefox: Range.createContextualFragment() — winner (fastest in Firefox) ... Note that results differ from test to test. However, the clear ’loser’ appears to be DOMParser. When adding multiple nodes at once, it is recommended to use a DocumentFragment as placeholder and append all nodes at once: const htmlToElement = (html) => ({ /* ...
🌐
Sabe
sabe.io › blog › javascript-convert-string-to-html
How to Convert a String to HTML using JavaScript - Sabe.io
December 4, 2022 - In this post, we learned how to convert a string to HTML using JavaScript. Simply use the built-in DOMParser class and call the parseFromString() method to convert your string into HTML.
🌐
Smashing Magazine
smashingmagazine.com › 2024 › 04 › converting-text-encoded-html-vanilla-javascript
Converting Plain Text To Encoded HTML With Vanilla JavaScript — Smashing Magazine
April 17, 2024 - We’ll set an onkeyup event handler on the <textarea> to call a JavaScript function called convert() that does what it says: convert the plain text into HTML. The conversion function should accept one parameter, a string, for the user’s plain ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript string to html
How to Convert String to HTML in JavaScript | Delft Stack
February 2, 2024 - Here, element refers to the DOM ... want to assign to the element. The innerHTML property can be utilized to convert a string into an HTML object by setting it with the desired string content....
🌐
Quora
quora.com › What-is-the-process-for-converting-an-HTML-string-into-a-DOM-object-using-JavaScript
What is the process for converting an HTML string into a DOM object using JavaScript? - Quora
Answer: Assigning the HTML string directly to an Element.innerHTML is probably the easiest and the most optimal way of doing it as all the work is done by the browser. E.g. elem.innerHTML = ‘ this is hello world Once the above statement is executed, the HTML string will ...
🌐
Actual Wizard
actualwizard.com › actual wizard › programming › javascript › how to convert an html node to a string in javascript
How to Convert an HTML Node to a String in JavaScript - Actual Wizard
October 5, 2020 - The big issue here is that an HTML document could have many elements with the same tag name, and it could be difficult to find the specific element that you are looking for by it’s tag. In most cases, you will likely want to use the outerHTML method on the node that your code has selected.