I am practising with DOM manipulation, in this case with .remove(). I get to find all the 'p' in my HTML and remove them, with a forEach function.
const ps = document.querySelectorAll('p')
ps.forEach(function (p) {
p.remove()
})
But for the shake of learning, I would like to transform that forEach into a for Loop, but I am not sure how to fill in the function to make it work . I am trying :
const ps = document.querySelectorAll('p')
for (i = 0; i < ps.length; i++) {
p.remove()
}
p[i].remove()in the case of afor..loopps[i].remove(), yeah, it worked :) thanks! would you care to explain why inside the forEach, it works with p.remove() ? is it because p is the argument passed inside function(p) ?pis not even defined in your second code. You could use it in your first code because it is a param in your forEach callback. You can do ps[i].psis a NodeList - thereforeps[i]