This commit is contained in:
Ilya Kantor 2017-02-28 12:54:48 +03:00
parent 4272b7bb13
commit 508969c13f
168 changed files with 340 additions and 10 deletions

View file

@ -0,0 +1,32 @@
First, let's see how not to do it:
```js
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` every time, so elements every time start from index `0`. So `i` should not increase in the loop at all.
The `for..of` loop also does the same.
The right variant would be:
```js
function clear(elem) {
while (elem.firstChild) {
elem.firstChild.remove();
}
}
```
And also there's a simpler variant:
```js
function clear(elem) {
elem.innerHTML = '';
}
```