Converting a JavaScript String Containing HTML Code into HTML Format

To simplify the process, one can simply utilize the best HTML parser available instead of attempting to parse the HTML string using RegExp. Follow these steps: create a new object, insert the HTML string, and use selectors to extract the desired information. This method can handle multiple tables with varying structures and multiple data rows within each table.


Solution 1:

One way to achieve an aesthetically pleasing solution is by utilizing DOMParser.

const parser = new DOMParser()
const virtualDoc = parser.parseFromString(htmlString, 'text/html')

Handle

virtualDoc

in the same way as you would deal with any other DOM element.

virtualDoc.getElementById('someid').value


Solution 2:


It’s uncertain how effective it is, but simply passing the entire response containing

<html>

and additional tags to a container seems to work. Afterwards, one can easily make a call.

document.getElementById("needid").value

Frequently Asked Questions