اÛÙ Ù
ØØªÙا تÙÙØ§ در اÛ٠زباÙâÙØ§ Ù
ÙØ¬Ùد است: English, Español, Français, Italiano, æ¥æ¬èª, íêµì´, Ð ÑÑÑкий, Türkçe, УкÑаÑнÑÑка, OÊ»zbek, ç®ä½ä¸æ. ÙØ·Ùا٠ب٠Ù
ا
Clear the element
اÙÙ
ÛØª: 5
Create a function clear(elem) that removes everything from the element.
First, letâs see how not to do it:
function clear(elem) {
for (let i=0; i < elem.childNodes.length; i++) {
elem.childNodes[i].remove();
}
}
That wonât work, because the call to remove() shifts the collection elem.childNodes, so elements start from the index 0 every time. But i increases, and some elements will be skipped.
The for..of loop also does the same.
The right variant could be:
function clear(elem) {
while (elem.firstChild) {
elem.firstChild.remove();
}
}
And also thereâs a simpler way to do the same:
function clear(elem) {
elem.innerHTML = '';
}