13

I am getting the following error in Google Chrome on a very old Form I inherited (seems to work OK in IE, no errors)

Uncaught TypeError: Cannot read property 'firstChild' of null

The error applies to the following js elem.firstChild.nodeValue = dispmessage;:

function msg(fld,     
         msgtype, 
         message) {
  if (emptyString.test(message))
    dispmessage = String.fromCharCode(nbsp);
  else
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;

  elem.className = msgtype;   // set the CSS class to adjust appearance of message
};

Wondering if someone has come across similar issue before? Any ideas on how I can resolve?

Cheers

user3445112
  • 201
  • 1
  • 2
  • 10

1 Answers1

21

This error means the elem object is null. Check the fld value passed and see whether an object with that id actually exists in your code or not.

ipohfly
  • 1,959
  • 6
  • 30
  • 57
  • 3
    or in simple ways: your javascript is loading way before your html and thus, javascript is not able to find the DOM Element. Cheers ;-) – Dynamic Remo May 06 '17 at 14:50