Skip to content

Instantly share code, notes, and snippets.

@avshabanov
Created January 11, 2017 01:59
Show Gist options
  • Save avshabanov/7d5adcb08e621300be3860cc2caaacb0 to your computer and use it in GitHub Desktop.
Save avshabanov/7d5adcb08e621300be3860cc2caaacb0 to your computer and use it in GitHub Desktop.
Append HTML String as DOM part
// NOTE: two approaches are possible here, one uses jquery abstractions (and works on pretty old browsers),
// while the other uses insertAdjacentHTML, the standard API in all the modern browsers (and old IEs too!).
//
// First approach (jquery way):
const $element = $($.parseHTML(htmlString));
$element.appendTo($container);
// Other approach (HTML, works on modern browsers):
$container.each(function () { this.insertAdjacentHTML('beforeend', htmlString); });
// Links:
// https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
// https://msdn.microsoft.com/en-us/library/ms536452(v=vs.85).aspx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment