Files
30-seconds-of-code/snippets/createElement.md
2018-01-06 03:21:44 +11:00

476 B

createElement

Creates an element from a string.

Use document.createElement() to create a new element. Set its innerHTML to the string supplied as the argument. Use ParentNode.firstElementChild to return the element version of the string.

const createElement = str => {
  const el = document.createElement('div');
  el.innerHTML = str;
  return el.firstElementChild;
};
createElement(
  `<div class="container">
    <p>Hello!</p>
  </div>`
);