Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 523
Prem Avatar asked Aug 22 '19 15:08

Prem


People also ask

How do you parse an object in HTMLDivElement?

Just clone the container whose nodes you want to copy and get the nodes from the copied one, if you want to make the original container intact. And you are putting them into the array and then running a loop multiple times instead you can run a loop once and simultaneously put them in your . slider class.

How do I Stringify an HTML element?

To convert a HTMLElement to a string with JavaScript, we can use the outerHTML property. const element = document. getElementById("new-element-1"); const elementHtml = element.

What is HTMLDivElement?

HTMLDivElement.align. A string representing an enumerated property indicating alignment of the element's contents with respect to the surrounding context. The possible values are "left" , "right" , "justify" , and "center" .


1 Answers

Use .outerHTML to get the HTML of the DIV.

<body>${doc.getElementById('vcr_data').outerHTML}</body>
like image 153
Barmar Avatar answered Sep 19 '22 12:09

Barmar