Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Uncaught TypeError: Cannot read property 'firstChild' of null [duplicate]

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

like image 508
user3445112 Avatar asked Mar 26 '14 03:03

user3445112


People also ask

What is cannot read properties of null (reading 'firstchild') in JavaScript?

The Uncaught TypeError: Cannot read properties of null (reading 'firstChild') is a common error in JavaScript. There can be two reasons behind occurring this error and the solutions to this problem are easy. The reasons behind occurring this problem are either you type a wrong id or you misplaced the script for linking the JS file into HTML.

What does cannot read property'value'of null mean?

Uncaught TypeError: Cannot read property 'value' of null Uncaught TypeError: Cannot read property 'innerHTML' of null All this means is that you are trying to access a property of an object that is undefined. These usually happens when we don't test an object before using it. Here is a common scenario.

Why does it say undefined instead of null in JavaScript?

Sometimes instead of null it will say undefined. An example will be: Uncaught TypeError: Cannot read property 'value' of null. Uncaught TypeError: Cannot read property 'innerHTML' of null. All this means is that you are trying to access a property of an object that is undefined. These usually happens when we don't test an object before using it.

What does null is not an object mean in JavaScript?

This error occurs when you read a property or call a method on a null object . That's because the DOM API returns null for object references that are blank. An object is expected somewhere and wasn't provided. So, you will get null is not an object error if the DOM elements have not been created before loading the script.


1 Answers

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.

like image 124
ipohfly Avatar answered Oct 29 '22 04:10

ipohfly