1

I have stored HTML code in a variable and would like to add it into the body of my page.

Example code:

let htmlCode = <div class="anyclass"> \n' +
'      <div id="dummyId" name="someName"> \n' +
'            <br> \n' +
'            <br> \n' +
'            <br> \n' +
'            <br> \n' +
'            <br> \n' +
'            <br> \n' +                     
'      </div> \n' +
'  </div>';

Tried to insert it into the body of the page like this:

e.target.appendChild(htmlCode);

The failure message is:

Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

What is the right way to add this code into the HTML structure? How do I transfer a variable into a node. Happy for some help. Thanks!

Harald
  • 243
  • 2
  • 12
  • [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+append+html+string) of [Appending HTML string to the DOM](/q/7327056/4642212) (also: [Append HTML element to the DOM instead of a string of HTML text](/q/53746340/4642212)). – Sebastian Simon Jun 19 '21 at 15:06
  • `e.target` doesn't have the type HTML [Node](https://developer.mozilla.org/en-US/docs/Web/API/Node). If you post the code from where the call is made to `e.target.appendChild(htmlCode)` it will be easy to help. – ThilankaD Jun 19 '21 at 17:53
  • 2
    Does this answer your question? [Error: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'](https://stackoverflow.com/questions/27079598/error-failed-to-execute-appendchild-on-node-parameter-1-is-not-of-type-no) – ThilankaD Jun 19 '21 at 17:55

0 Answers0