0

I have javascript code that manages an HTML table. The code needs to be able to delete a row from an HTML table.

I am currently using the following code to delete the row:

var rowToDelete = ...;

if (rowToDelete)
    rowToDelete.remove();

This works fine in Firefox & Chrome, however, when I load the page in IE 11 & bring up the javascript debugger, it displays

Object doesn't support property or method 'remove'

I haven't tried this code in earlier versions of IE yet.

How can I do this in a cross-browser manner? My page does have jQuery included so I can use a jQuery method.

8
  • 6
    what is rowToDelete? is it a dom element or jQuery wrapper object Commented Dec 20, 2013 at 15:58
  • Is rowToDelete a jQuery object, or a node? Commented Dec 20, 2013 at 15:58
  • ...? in var rowToDelete = ...; Commented Dec 20, 2013 at 15:59
  • show us the full code please. Commented Dec 20, 2013 at 15:59
  • 2
    It is a Node element. Chrome supports .remove() on elements. He should do rowToDelete.parentNode.removeChild(rowToDelete); Commented Dec 20, 2013 at 16:00

3 Answers 3

3

Chrome supports .remove() on elements.
You should do:

rowToDelete.parentNode.removeChild(rowToDelete);

If you want this functionality in IE you can add the function to the HTMLElement prototype.

HTMLElement.prototype.remove = function (){
    this.parentNode.removeChild(this);
}
Sign up to request clarification or add additional context in comments.

3 Comments

sorry, did not understand at the first instance.. +1 for this
Interestingly, FF doesn't seem to return the removed element like Chrome does. I'm pretty sure the spec requires this. I'll have to go back and check.
...looks like undefined is the correct result. I guess it makes sense since you'd already have the node.
1

Make sure your rowToDelete is an jQuery Object, like this:

var rowToDelete = $('tr');
rowToDelete.remove();

1 Comment

rowToDelete was a DOM element, but I did have easy access to a jQuery object for the same DOM element without having to call jQuery again to get one. This works. Thanks.
0

If it is a dom element you can do

$(rowToDelete).remove();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.