UNPKG

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

22 lines (21 loc) 610 B
import isString from 'vanillajs-helpers/isString'; import ensureHTML from './ensureHTML'; /** * Append DOM element or plain HTML to the end of a given DOM element * * @param elm - The DOM element to append to * @param insertElm - DOM element or HTML (or selector) to append to insert * @return The inserted child element */ export default function append(elm, insertElm) { if (!elm) { return null; } if (isString(insertElm)) { elm.insertAdjacentHTML('beforeend', ensureHTML(insertElm)); } else { elm.append(insertElm); } return elm.lastElementChild; }