Skip to main content

Create a Document Fragment from an HTML string.

/**
 * Create a Document Fragment from an HTML string
 *
 * @param  {String} htmlString HTML to convert to DocumentFragment.
 *
 * @return {Document Fragment} A Document Fragment representing input HTML.
 */

function createDocumentFragmentFromHTML(htmlString) {
  var element = document.createElement('div'),
    result = document.createDocumentFragment();

  element.innerHTML = htmlString;

  while (element.hasChildNodes()) {
    result.appendChild(element.firstChild);
  }

  return result;
}