2021年4月12日星期一

how to remove all element with a specific innerHTML

function checkIfBlank() {    let li = document.getElementsByClassName("li");    for (var i = 0; i < li.length; i++) {    if (li[i].innerHTML === "hello") {      li[i].parentElement.remove();    }    }  }  
<li>      <span class="li">hi</span>  </li>  <li>      <span class="li">hi</span>  </li>  <li>      <span class="li">hi</span>  </li>  <li>      <span class="li">hello</span>  </li>  <li>      <span class="li">hello</span>  </li>  <li>      <span class="li">hello</span>  </li>  

i have this function that will remove parent element of the span that has innerHTML of "hello"

In this case, I expect the code to remove all elements with innerHTML of hello

but turns out it only removes one element when function is executed

can anybody help me on this? im not really good xD

https://stackoverflow.com/questions/67068678/how-to-remove-all-element-with-a-specific-innerhtml April 13, 2021 at 12:16PM

没有评论:

发表评论