Home > Software engineering >  Trying to eliminate children 3-7 from an element, I am looping through the array to eliminate them b
Trying to eliminate children 3-7 from an element, I am looping through the array to eliminate them b

Time:02-25

So first main has only one child which is why I selected the child blades as var blades = main.children[0]; then I am trying to loop through and just eliminate the children within blades from [3] through [7] but for some reason when doing this on the console it won't work.

 function deleteBlades() {
        var main = document.getElementById("id-9a1e3f8f48548d4c2c422b1f92cb749a");
        var blades = main.children[0];
        for (var i = 0; i < blades.length; i  ) {
            if( i >= 3 || i <= 7)
                {
                    blades.children[i].remove();
                }
        }
    }
    deleteBlades();

Any ideas on what am I doing wrong?

CodePudding user response:

Looks like you got a typo in your loop.

I think what you meant to do is:

for (var i = 0; i < blades.children.length; i  ) {

CodePudding user response:

in your for loop you are reading blades.length when it is a HTMLElement not array, use blades.children.length instead

 function deleteBlades() {
    var main = document.getElementById("id-9a1e3f8f48548d4c2c422b1f92cb749a");
    var blades = main.children[0];
    for (var i = 0; i < blades.children.length; i  ) {
        if( i >= 3 || i <= 7)
            {
                blades.children[i].remove();
            }
    }
}
deleteBlades();
  • Related