0

I m making some project.

When I click "number1" button, I thought "number2" and "drag-zone" box is removed at once

But It's not happened. I have to click "number1" to three time to remove "the number2" and "drag-zone"

I want to remove that at once. What's my problem in my code?

I don't know How could I remove NodeList at once

let list = document.getElementsByClassName("boxDrop");


function remove(num) {

  if (num === 0) {
    for (let i = 0; i < list[1].childNodes.length; i++)
      list[1].removeChild(list[1].childNodes[i]);
  }

}
* {
  align-items: center;
  text-align: center;
  justify-content: space-evenly;
}

button {
  border: none;
  background-color: transparent;
  font-size: 20px;
  border-radius: 10px;
}

button:hover {
  background-color: rgb(252, 211, 77);
}

.thumbDropBox {
  display: flex;
  border: 3px solid rgb(209, 178, 2);
  padding: 15px;
  margin-top: 100px;
  cursor: pointer;
}

.drop-zone {
  display: flex;
  border: 3px dashed rgb(253, 196, 39);
  margin: 10px;
  width: 200px;
  height: 200px;
  padding: 20px;
  font-size: 30px;
  font-weight: bold;
  border-radius: 14px;
}

.drop-zone__over {
  border-style: solid;
}

.drop-zone__input {
  display: none;
}

.drop-zone__thumb {
  background-color: rgb(235, 235, 235);
  height: 100%;
  width: 100%;
  border-radius: 15px;
  overflow: hidden;
  position: relative;
}

.drop-zone__thumb::after {
  content: attr(data-label);
  width: 100%;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  background-color: rgb(192, 192, 192);
  position: absolute;
  bottom: 0;
}

.question {
  display: inline-block;
  padding: 20px;
  font-size: 30px;
}
<div class="thumbDropBox">
  <div class="boxDrop">
    <div class="drop-zone">
      <span class="drop-zone__prompt">Drag or Choose</span>
      <input type="file" class="drop-zone__input">
      <!-- <div class="drop-zone__thumb" data-label="eskdflsdf.txt"></div> -->
    </div>
    <button onclick="remove(0)">Number1</button>
  </div>

  <div class="boxDrop">
    <div class="drop-zone">
      <span class="drop-zone__prompt">Drag or Choose</span>
      <input type="file" class="drop-zone__input">
      <!-- <div class="drop-zone__thumb" data-label="eskdflsdf.txt"></div> -->
    </div>
    <button onclick="remove(1)">Number2</button>
  </div>
</div>

<span class="question">Whose is the Better?</span>
FishKing
  • 59
  • 4
  • 1
    This answer may help you. https://stackoverflow.com/a/3955238/14032355 – ikhvjs Apr 07 '21 at 06:43
  • 2
    _"The [`Node.childNodes`](https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes) read-only property returns a **live** `NodeList` of child nodes of the given element where the first child node is assigned index 0."_ - The important part is in bold -> the content of `.childNodes` will be updated if the DOM changes. If you remove a child, then the content of `.childNodes` changes - but you never update `i` accordingly. The easiest solution, if you want to keep that loop, is to remove the elements from the back. – Andreas Apr 07 '21 at 06:43

0 Answers0