7

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);

1 Answer 1

16

Use .outerHTML to get the HTML of the DIV.

<body>${doc.getElementById('vcr_data').outerHTML}</body>
Sign up to request clarification or add additional context in comments.

1 Comment

And in case anyone's trying to do this with the Click Element variable in Google Tag manager like I was: Use the dataLayer variable gtm.element.outerHTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.