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
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript string to html
How to Convert String to HTML in JavaScript | Delft Stack
February 2, 2024 - The string to HTML object conversion ... the best way to implement this task is to create a div element and pass the string (HTML pattern for a new element) via innerHTML....
Discussions

converting a javascript string to a html object - Stack Overflow
can I convert a string to a html object? like: string s = ' '; var htmlObject = s.toHtmlObject; so that i can later on get it by id and do some changing in its sty... More on stackoverflow.com
🌐 stackoverflow.com
April 27, 2017
javascript - Convert a string to html tags - Salesforce Stack Exchange
I'm fairly new to LWC development so please let me know if I'm heading the wrong way here. I want to convert a string of html tags to html and to render it on my lightning component. I followed the More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
October 20, 2019
Convert string to HTML element using JavaScript or jQuery - Stack Overflow
I have a string like this: i1:1,i3:5,i2:5 How can I convert it to a HTML element like this using pure JavaScript or jQuery? ... More on stackoverflow.com
🌐 stackoverflow.com
June 18, 2021
Parse html elements which are contained in a string
You can use DOMParser() const parser = new DOMParser(); const html = parser.parseFromString(your_article_string, 'text/html'); Then you can just add the html element as a child of element element. More on reddit.com
🌐 r/learnjavascript
3
1
July 26, 2023
🌐
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 - /** * Convert a template string ... goes here... }; The simplest way to do this is to create an element, insert the string into with innerHTML, then return the element....
🌐
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 - const cleanHTMLString = string => string .trim() .replace(/\r?\n|\r/g, "") // removes multiple lines .replace(/\s\s+/g, '') // removes extra white spaces
🌐
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 text input entered into the <textarea> element:
🌐
Sabe
sabe.io › blog › javascript-convert-string-to-html
How to Convert a String to HTML using JavaScript - Sabe.io
December 4, 2022 - JAVASCRIPTconst htmlString = ` <main> <h1>Convert a String to HTML</h1> <p>Learn how to convert a string to HTML using JavaScript.</p> </main> `; const convertStringToHTML = htmlString => { const parser = new DOMParser(); const html = parser.parseFromString(htmlString, 'text/html'); return html.body; } const body = convertStringToHTML(htmlString); console.log(body);
🌐
Codegyan
codegyan.in › articles › how-to-convert-a-string-to-html-using-javascript.htm
How to Convert a String to HTML using JavaScript
December 30, 2021 - JavaScript, on the other hand, ... most straightforward method for converting a string to HTML is by leveraging the `innerHTML` property of an HTML element....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-create-an-element-from-a-string-in-javascript
Creating an element from a string in JavaScript - GeeksforGeeks
February 25, 2026 - Using innerHTML: Create a container element and set its innerHTML to the string, then access the created element. Using insertAdjacentHTML(): Directly inserts HTML string into the DOM at a specified position without overwriting existing content.
🌐
Daily Dev Tips
daily-dev-tips.com › posts › javascript-get-html-elements-from-a-string
JavaScript get HTML elements from a string
December 22, 2020 - To get started, let’s first define our HTML. We will be using a variable, which you can consider came from our CMS. const text = `<p>Some text</p><br /><a href="https://daily-dev-tips.com/">My website</a><hr /><a href="https://google.com">Another link</a>`; As you can see, we have two links in there. Let’s say we want to parse each link to add a target="_blank". We can leverage the DOMParser to convert this string into a dom element.
🌐
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.
🌐
CSS-Tricks
css-tricks.com › snippets › javascript › inject-html-from-a-string-of-html
Inject HTML From a String of HTML | CSS-Tricks
March 19, 2020 - let dom = new DOMParser() .parseFromString(string_of_html, 'text/html'); let new_element = dom.body.firstElementChild; Now you can mess around with that new_element as needed, I suppose before doing what you need to do with it.
🌐
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 ...
🌐
SheCodes
shecodes.io › athena › 5274-converting-an-array-of-strings-into-html-using-javascript
[JavaScript] - Converting an Array of Strings Into HTML Using JavaScript
Learn how to use JavaScript's `map()` method to iterate over an array of strings and turn them into HTML elements to be rendered on a page.
🌐
Krasimirtsonev
krasimirtsonev.com › blog › article › Convert-HTML-string-to-DOM-element
Convert HTML string to DOM element
June 11, 2013 - The styling of the elements inside didn't work and sometimes some of the them weren't rendered at all. As normally happen I ended with a little dirty hack. var str2DOMElement = function(html) { var frame = document.createElement('iframe'); frame.style.display = 'none'; document.body.appendChild(frame); frame.contentDocument.open(); frame.contentDocument.write(html); frame.contentDocument.close(); var el = frame.contentDocument.body.firstChild; document.body.removeChild(frame); return el; } var markup = 'text here'; var el = str2DOMElement(markup);