Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'

Viewed 408

I just noticed an issue with my chat extension for phpBB. I've had some recent memory issues and just can't get it figured out. If someone could lend a hand it would be greatly appreciated

the js with an issue...

            } else if (type == 'delete') {
            var parent = document.getElementById('chat');
            var child = document.getElementById('p' + results[0]);
            parent.removeChild(child);
        }

if anyone needs to see the full js. just ask and i will post it

2 Answers

Try to check if the child exists before removal:

} else if (type == 'delete') {
  var parent = document.getElementById('chat');
  var child = parent.getElementById('p' + results[0]);
  if (child) parent.removeChild(child);
}

removeChild is a method of Node, this is what the error message tells you with

'removeChild' of 'Node'

the first parameter is child and the error message tells you that it's not a Node.

getElementById returns the element having that id if exists. If not, the null is returned. Node is the parent interface of Element.

Since it's not a Node, it is null. Hence child does not exist.

Related