append raw html javascript 1

append raw html javascript

function appendHtml(el, str) {
  var div = document.createElement('div');
  div.innerHTML = str;
  while (div.children.length > 0) {
    el.appendChild(div.children[0]);
  }
}
var html = '

Some Title

Some arbitrary text'; appendHtml(document.body, html); // "body" has two more children - h1 and span.

Here is what the above code is Doing:
1. Create a div element.
2. Set the div’s innerHTML to the HTML string.
3. Iterate over the div’s children and append each one to the element.
4. Remove the div from the DOM.

Similar Posts