removeAllChildren.js 503 B

12345678910111213141516171819
  1. /**
  2. * Remove all children of an element.
  3. * @param {HTMLElement} element A valid HTML element.
  4. * @param {number} [skip] Number of elements to skip removing.
  5. * @returns {void}
  6. */
  7. function removeAllChildren(element, skip) {
  8. /** @type {Node[]} */
  9. const childList = Array.prototype.slice.call(
  10. element.childNodes,
  11. typeof skip !== 'undefined' ? skip : 0
  12. );
  13. for (let i = 0; i < childList.length; i += 1) {
  14. element.removeChild(childList[i]);
  15. }
  16. }
  17. module.exports = removeAllChildren;