2

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

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Darin
  • 35
  • 4
  • Looks like a duplicate of https://stackoverflow.com/questions/42956884/failed-to-execute-removechild-on-node – automaticAllDramatic Mar 16 '20 at 23:51
  • the solution that helped HerrimanCoder does not work in my case so i don'tt see it as a duplicate. similar maybe but not the same – Darin Mar 17 '20 at 14:01

2 Answers2

2

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);
}
Kosh
  • 16,966
  • 2
  • 19
  • 34
  • i'm accepting your post as the answer. however, the only line i changed to make it work without error is the last line `if (child) parent.removeChild(child);`. thank you very much – Darin Mar 17 '20 at 14:04
  • @Darin, yes, that line removes the error. Another one `var child = parent.getElementById('p' + results[0]);` searches the child within the parent, not within whole document. This improves the performance and may be optional. – Kosh Mar 17 '20 at 15:40
0

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.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • thank you for your explanation but it was not helpful at all. like i said in my post, i've had some recent memory issues and i do use google first before asking for help. i had already read what you posted but am unable to understand it since my memory issues happened. what Kosh posted helped me solve the issue or at least stop the error completely without any loss of function – Darin Mar 17 '20 at 14:26