0

This is my code; If the ip variable is null, it does not return an error.

script;

function addIP(ip)  {
var PRE = document.createElement('PRE');
PRE.textContent = ip;
document.getElementById('Leak').appendChild(PRE);
document.getElementById('p1').remove();}

html;

<td id="Leak"><pre id="p1">No Leak</pre></td>

error in console;

Uncaught TypeError: Cannot read property 'remove' of null

EDIT; solved in this way.

var node = document.getElementById ('p1');
if (node !== null) {document.getElementById('p1').remove();}
pavele
  • 1
  • 1
  • `addIP` is being called when the `p1` element is not in the DOM, see the linked question's answers for details. That could be because it's being called before that part of the HTML has been parsed and the elements added to the DOM, or more likely in this case because `addIP` has been called at least once before. – T.J. Crowder May 12 '19 at 14:57
  • Make sure your DOM is valid. your `td` element should be inside a `` and ``.
    – Aᴍɪʀ May 12 '19 at 14:58
  • @Aᴍɪʀ already so. – pavele May 12 '19 at 15:19
  • thx for help solved. – pavele May 12 '19 at 16:23

0 Answers0