Removing an element through the parentNode.removeChild throws a DOM Exception 8

Viewed 8984

I have code which is roughly as follows (I removed some parts, as they are irrelevant):

Library.focus = function(event) {
    var element, paragraph;

    element = event.srcElement;

    paragraph = document.createElement("p");
    paragraph.innerText = element.innerText;

    element.parentNode.insertBefore(paragraph, element);        // Line #1
    element.parentNode.removeChild(element);                    // Line #2
};

The issue I have, is that the last call which I've numbered as line #2 throws this:

Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

Note that the previous line #1 works fine, and inserts the paragraph node before it. Element is an existing element, and both element.parentNode and element.parentNode.removeChild exist as well.

I find this illogical, as element is by definition a child of its parentNode. Maybe StackOverflow will be able to help me out, here?

3 Answers
Related