Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery this.remove() -vs.- $('#id').remove() in Internet Explorer (IE 9+)

Why is it that this.remove() doesn't work in IE9+?

<input type="button" value="Next1" id="nextButton1">
<br>
<input type="button" value="Next2" id="nextButton2">

$('#nextButton1').on('click', function() {
   this.remove(); // works in all browsers but IE9+
});

$('#nextButton2').on('click', function() {
   $('#nextButton2').remove(); //works in all browsers
});

JSFiddle live version

like image 783
pemoke Avatar asked Jun 18 '14 02:06

pemoke


2 Answers

That's because you're using the ChildNode.remove() method which is not supported by all browsers.

this ---> refers to a node. //WARNING: This is an experimental technology

jQuery's .remove() method, however is cross-browser and so to use it you have to wrap this in $(...) like this:

$(this).remove();

ChildNode.remove() Browser Compatibility

this.remove() is supported by the following desktop browsers:

- Chrome 23+
- Firefox 23+
- Opera 10+
- Safari 7+
like image 59
PeterKA Avatar answered Oct 21 '22 14:10

PeterKA


Wrap this in jQuery:

$('#nextButton1').on('click', function() {
   $(this).remove();
});
like image 27
Joseph Silber Avatar answered Oct 21 '22 14:10

Joseph Silber