-1

I have a dom now which is like

span
span
p
p(to be removed)
p(to be removed)

and everytime a event was triggered, I d like to remove the last two p elements and append 2 more.

There is a node.removeChild method, but I tried, which turned out that the array of p is not the father element of p, but I do not know whether I did it the whole wrong way.

So, how could I remove the last two p nodes in the array of p, so I could clean the space and append two more?

royhowie
  • 11,075
  • 14
  • 50
  • 67
GLPease
  • 545
  • 1
  • 8
  • 19

3 Answers3

1

You probably want

el.parentNode.removeChild(el);

The parentNode property will allow you to use removeChild in the way you intended to.

nkorth
  • 1,684
  • 1
  • 12
  • 28
0

Thanks for the answers, I finally made it myself.

The childNode.remove() method.

for (var index = here is the length you wanna reserve; index < the whole  array length; index++) {
    obj[index].remove();
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
GLPease
  • 545
  • 1
  • 8
  • 19
-1

declare a counter i, then check if it's between [number of p - 2 .. number of p]

var i = 0;
$("p").each(function(){
    if(i>=$("p").length-2)
         $(this).remove();
    i++;
});
Elheni Mokhles
  • 3,801
  • 2
  • 12
  • 17