Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript remove all elements with name

Tags:

javascript

I am trying to use JavaScript to remove all the elements with a certian name, but it is only removing the first one.

My code is:

var ele= document.getElementsByName("javascriptaudio");
for(var i=0;i<ele.length;i++)
{
  ele[i].parentNode.removeChild(ele[i]);
}

Can anyone tell me what is wrong?

Thanks

like image 279
user2370460 Avatar asked Oct 24 '25 08:10

user2370460


1 Answers

I don't have enough rep to comment on Álvaro G. Vicario. The reason that it works is that the element is removed from ele when it is removed from the DOM. Weird.

The following code should work equally well:

var ele= document.getElementsByName("javascriptaudio");
len = ele.length;
parentNode = ele[0].parentNode;
for(var i=0; i<len; i++)
{
  parentNode.removeChild(ele[0]);
}
like image 59
Andreas Kalin Avatar answered Oct 26 '25 23:10

Andreas Kalin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!