function delete(){
let k = document.getElementsByClassName('row');
for(let i=0; i<k.length; i++)
{
if(k[i].hasChildNodes()){
k[i].removeChild(k[i].childNodes[2]);
}
}
}
<div id="table">
<div class="row">
<div class="column">1.1</div>
<div class="column">1.2</div>
<div class="column">1.3</div>
<div class="column">1.4</div>
<div class="column">1.5</div>
</div>
<div class="row">
<div class="column">2.1</div>
<div class="column">2.2</div>
<div class="column">2.3</div>
<div class="column">2.4</div>
<div class="column">2.5</div>
</div>
<div class="row">
<div class="column">3.1</div>
<div class="column">3.2</div>
<div class="column">3.3</div>
<div class="column">3.4</div>
<div class="column">3.5</div>
</div>
<div class="row">
<div class="column">4.1</div>
<div class="column">4.2</div>
<div class="column">4.3</div>
<div class="column">4.4</div>
<div class="column">4.5</div>
</div>
<div class="row">
<div class="column">5.1</div>
<div class="column">5.2</div>
<div class="column">5.3</div>
<div class="column">5.4</div>
<div class="column">5.5</div>
</div>
</div>
<div id="button">
<input type="button" onclick="delete()" value="delete">
</div>
Trying to remove every second column using the method removeChild() in JavaScript, facing an error when there are no remaining children to delete. The console displays "
Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'"
when all columns are already removed.
Question: How to handle this error condition? Can I use if(!k[i].hasChildNodes())
or another method to check if there are no more children before deleting? How would I go about implementing a condition to handle this situation?