vanillajs-browser-helpers
Version:
Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser
16 lines (12 loc) • 377 B
text/typescript
import insertAfter from './insertAfter';
function findEmptyElm(elm?: Element | null): Element | null | undefined {
const child = elm?.firstElementChild;
return child ? findEmptyElm(child) : elm;
}
export default (elm: Element, html: string) => {
if (html) {
const wrapElm = findEmptyElm(insertAfter(elm, html));
wrapElm?.appendChild(elm);
}
return elm;
};