I am doing an assignment where I'm meant to remove a paragraph from the page when a button is clicked. I keep returning an error "Uncaught TypeError: Node.removeChild: Argument 1 is not an object." and it references line 37 in my code.
Line 37:
divTag.removeChild(divTag.children[allPs.length-1]);
This is part of a script:
btnDelete.addEventListener('click', function(){
var allPs = document.querySelectorAll('p');
divTag.removeChild(divTag.children[allPs.length-1]);
});
One important thing to consider is that divTag.children, which looks at the children of one element in the DOM, may be considerably shorter than allPs.length, which as you've defined it is all the p elements in the DOM. What's probably actually causing the error is that you're looking at an index ([allPs.length - 1]) that doesn't exist in the array divTag.children. I don't know the exact spec of what you're working on, but you can try replacing divTag.children[allPs.length - 1] with allPs[allPs.length - 1]. Without more info on what the actual spec is that's about as helpful as I can be for now. If you have more info I can elaborate in a comment.