53

I have the following code in JavaScript:

all_el_ul = document.getElementsByClassName('element_list')[0];
div_list = all_el_ul.getElementsByTagName("div");
for (i = 0; i < div_list.length; i += 1) {         
  div_list[i].remove();             
}

I know that this is the problem because I used alert('test'); to see where the code stops working. Everything is working in FF, Chrome, Opera and others but not in IE.

Could you please tell what is wrong?

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
AlexPac
  • 620
  • 1
  • 5
  • 8
  • possible duplicate of [JQuery remove div not working in Internet Explorer](http://stackoverflow.com/questions/9443470/jquery-remove-div-not-working-in-internet-explorer) – Laurent S. Dec 06 '13 at 16:32
  • Check the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode.remove): *"Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the spec changes."* – Felix Kling Dec 06 '13 at 16:32
  • @Bartdude: No, that's not the problem here. – Felix Kling Dec 06 '13 at 16:33
  • @AlexPac Could you please mark one of the anwser as resolved ? – Antoine Subit Mar 05 '19 at 09:30

5 Answers5

92

IE doesn't support remove() native Javascript function but does support removeChild().

Browser compatibility for remove()

Desktop browser compatipility for remove() function

Mobile browser compatipility for remove() function

Solution n°1

Use remove() in pure Javascript you can declare it yourself with this following code :

// Create Element.remove() function if not exist
if (!('remove' in Element.prototype)) {
    Element.prototype.remove = function() {
        if (this.parentNode) {
            this.parentNode.removeChild(this);
        }
    };
}
// Call remove() according to your need
child.remove();

As you can see the function get the parent node of element and then remove this element from his parent with removeChild() native function.

Solution n°2

Use removeChild() in pure JavaScript on all browser including IE just call it insteed of remove().

element.removeChild(child);

More info on Mozilla Developer Network.

Solution n°3

Use jQuery through code.jquery.com CDN by using this following code :

<!-- Include jQuery -->
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
<!-- Use remove() -->
<script>
child.remove();
</script>

The function is included in the jQuery library so you can call it after inclusion.

Happy coding ! :-)

Antoine Subit
  • 9,803
  • 4
  • 36
  • 52
25

The native childNode.remove() is a new experimental method that is not is supported in Internet Explorer, only in Edge

Compatibility table from MDN

enter image description here enter image description here

You could use the more widely supported Node.removeChild instead

var all_el_ul = document.getElementsByClassName('element_list')[0];
var div_list  = all_el_ul.getElementsByTagName("div");

for (i = 0; i < div_list.length; i += 1) {         
   div_list[i].parentNode.removeChild(div_list[i]);             
}

or use the polyfill from MDN to add support for all browsers

(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        this.parentNode.removeChild(this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);

There is also a remove() method in jQuery, that does work cross-browser, but that would require adding the jQuery library.

$('.element_list').first().find('div').remove();

As a sidenote getElementsByClassName only works from IE9 and up, using querySelector would add IE8 support as well

var all_el_ul = document.querySelector('.element_list');
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • @FelixKling - just noticed it in the comments, never used it before, and didn't even know it existed – adeneo Dec 06 '13 at 16:33
  • 4
    @buzzsawddog: No, MDN is (mostly) valid for all browsers. And it explains why it works in the other browsers but not IE. Check the compatibility table. – Felix Kling Dec 06 '13 at 16:34
  • @FelixKling Okay checked the table that you supplied and there is a BOLD RED text that said "Not Supported" under the column for IE... – buzzsawddog Dec 06 '13 at 16:35
  • Thank you ! I have to learn the difference between javascript and jquery. :) – AlexPac Dec 06 '13 at 16:36
  • 1
    @buzzsawddog: Yes. The question was *"Everything is working in FF, Chrome, Opera and others but not in IE. Could you please tell what is wrong?"* The answer: It's not supported in IE. adeneo's original answer was that `.remove()` does not exist at all, which was incorrect. It does exist, but not in IE. – Felix Kling Dec 06 '13 at 16:36
  • @FelixKling - there's still a high probability that the OP confused jQuery with plain JS, as I don't think I've ever really seen the plain JS remove() method used in the wild. – adeneo Dec 06 '13 at 16:38
  • @adeneo: True. Might be a lucky coincidence that it worked in the other browsers. Maybe you should still add paragraph about the jQuery confusion to your answer. – Felix Kling Dec 06 '13 at 16:39
  • It's the same syntax, and the native method is supported in webkit and gecko? Who knows, but using the old parentNode.removeChild should work everywhere. – adeneo Dec 06 '13 at 16:40
  • The MDN polyfill is the only solution that worked for me and is supported by IE 5 and up. – Timm Jun 15 '20 at 21:36
16

Try adding this to the top of your JavaScript file:

if (!('remove' in Element.prototype)) {
  Element.prototype['remove'] = function () {
    if (this.parentNode) {
      this.parentNode.removeChild(this);
    }
  };
}

It is a small Element.remove() polyfill.

Add that to your JS and [element].remove() should magically start working in IE.

Daniel Tonon
  • 9,261
  • 5
  • 61
  • 64
karim essouabni
  • 161
  • 1
  • 3
6

Here is what I had to do for it to work in IE

if (typeof textField.remove === 'function') {
  textField.remove();
} else {
  textField.parentNode.removeChild(textField);
}
meda
  • 45,103
  • 14
  • 92
  • 122
3

Please try this. (Support all browsers)

var child = document.getElementById(id);
child.parentNode.removeChild(child);
Vijay Prajapati
  • 738
  • 1
  • 7
  • 20