/* DOM.js - A library of useful string functions for JavaScript 2021 - 2022 by mercury0x0d, Creative Commons Attribution 4.0 International License Revision history: 2022-01-16 - Added DOMChildrenRemoveAllButOne() 2021-12-14 - Initial release */ function DOMChildrenRemoveAll(element) { // removes all children from the element specified // if we've been given a string, convert it to an element if (typeof(element) == "string") element = document.getElementById(element); // repeatedly delete children while there are any left to remove while (element.children.length) { element.children[0].remove(); } } function DOMChildrenRemoveAllButOne(element) { // removes all children from the element specified // if we've been given a string, convert it to an element if (typeof(element) == "string") element = document.getElementById(element); // repeatedly delete children while there are any left to remove while (element.children.length > 1) { element.children[0].remove(); } }