Skip to content

Home

Create HTML element

Creates an element from a string (without appending it to the document). If the given string contains multiple elements, only the first one will be returned.

const createElement = str => {
  const el = document.createElement('div');
  el.innerHTML = str;
  return el.firstElementChild;
};

const el = createElement(
  `<div class="container">
    <p>Hello!</p>
  </div>`
);
console.log(el.className); // 'container'

More like this

Start typing a keyphrase to see matching snippets.