Prem
Prem

Reputation: 5997

How to stringify [object HTMLDivElement] to html string in javascript

I have written following code to find and get the div element with id vcr_data from html string. When I try to append that div to html tag, it's getting printed as follows instead of the stringified div:

... <body>[object HTMLDivElement]</body></html>

code snippet:

let doc = new DOMParser().parseFromString(html, 'text/html');
console.log('div element ', doc.getElementById('vcr_data'));

const response = `<!DOCTYPE html>
  <html>
    <head>
      <style id="jss-server-side"></style>
    </head>
    <body>${doc.getElementById('vcr_data')}</body>
  </html>`;

console.log('html: ', response);

Upvotes: 7

Views: 6431

Answers (1)

Barmar
Barmar

Reputation: 781350

Use .outerHTML to get the HTML of the DIV.

<body>${doc.getElementById('vcr_data').outerHTML}</body>

Upvotes: 16

Related Questions