Problem with your code is when you remove items, children will actually reduce, so you are shifting everything down one index. People typically loop backwards to stop this error.
Perosnally I would just use a empty pseudo class selector with querySelectorAll and a forEach loop.
var emptyAnchors = document.querySelectorAll("li > a:empty")
emptyAnchors.forEach(function (a) {
a.parentNode.remove()
})
<ul class="parent">
<li>
<a target='_blank' href="">One</a>
</li>
<li>
<a target='_blank' href=""></a>
</li>
<li>
<a target='_blank' href=""></a>
</li>
<li>
<a target='_blank' href="">Two</a>
</li>
<li>
<a target='_blank' href="">Three</a>
</li>
</ul>
When you're looping through a list and removing things, it throws the index off. Loop through it backwards instead:
let lis = document.querySelector(".parent").children
for (let i = lis.length - 1; i >= 0; i--) {
if (lis[i].firstElementChild.innerHTML === "") {
lis[i].remove()
}
}
<ul class="parent">
<li><a target='_blank' href="">One</a></li>
<li><a target='_blank' href=""></a></li>
<li><a target='_blank' href=""></a></li>
<li><a target='_blank' href="">Two</a></li>
<li><a target='_blank' href="">Three</a></li>
</ul>
<script>
//list of all anchor elements in your HTML DOM
onload = function() {
var anchors = document.getElementsByTagName("a");
for(var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
if(anchor.innerHTML == "") {
anchor.parentNode.removeChild(anchor); //removes the anchor
}
}
}
</script>
<a target='_blank' href="x">Something</a>
<a target='_blank' href="x"></a>
<a target='_blank' href="x">Something</a>
<a target='_blank' href="x">Something</a>
I'm building a Node.js app (Node v10.11.0) and want to do it in a OOP-way without using Typescript. So I've built some classes like this: class Component { constructor(param1, param2, param3) { ...
I'm building a Node.js app (Node v10.11.0) and want to do it in a OOP-way without using Typescript. So I've built some classes like this: class Component { constructor(param1, param2, param3) { ...
So I am trying to build my first react.js app and am wondering, why my page does neither update after POSTING a new player to my app via axios, nor after DELETING one. As I refresh the page afterwards ...
So I am trying to build my first react.js app and am wondering, why my page does neither update after POSTING a new player to my app via axios, nor after DELETING one. As I refresh the page afterwards ...
In JQuery I know that there is a function called keydown, but this does allow me to set a timeout each time the callback is called. $(document).keydown(() => { //do stuff }); The //do stuff ...
In JQuery I know that there is a function called keydown, but this does allow me to set a timeout each time the callback is called. $(document).keydown(() => { //do stuff }); The //do stuff ...
I'm trying to get text to show up in an SVG element I created. I created a textNode and then appended that to a SVG text element, but it doesn't seem to be showing up. The SVG element is showing up ...
I'm trying to get text to show up in an SVG element I created. I created a textNode and then appended that to a SVG text element, but it doesn't seem to be showing up. The SVG element is showing up ...