It says that TypeError: document.getElementById(...) is null

143,991

Solution 1

All these results in null:

document.getElementById('volume');
document.getElementById('bytesLoaded');
document.getElementById('startBytes');
document.getElementById('bytesTotal');

You need to do a null check in updateHTML like this:

function updateHTML(elmId, value) {
  var elem = document.getElementById(elmId);
  if(typeof elem !== 'undefined' && elem !== null) {
    elem.innerHTML = value;
  }
}

Solution 2

Make sure the script is placed in the bottom of the BODY element of the document you're trying to manipulate, not in the HEAD element or placed before any of the elements you want to "get".

It does not matter if you import the script or if it's inline, the important thing is the placing. You don't have to put the command inside a function either; while it's good practice you can just call it directly, it works just fine.

Solution 3

It means that the element with the id passed to getElementById() does not exist.

Solution 4

You can use JQuery to ensure that all elements of the documents are ready before it starts the client side scripting

$(document).ready(
    function()
    {
        document.getElementById(elmId).innerHTML = value;
    }
);

Solution 5

I got the same error. In my case I had multiple div with same id in a page. I renamed the another id of the div used and fixed the issue.

So confirm whether the element:

  • exists with id
  • doesn't have duplicate with id
  • confirm whether the script is called
Share:
143,991
mcan
Author by

mcan

Updated on January 20, 2022

Comments

  • mcan
    mcan over 2 years

    Althought I pushed a parameter to getElementById I wonder from where is this 'is null' error coming from?

    TypeError: document.getElementById(...) is null
    [Break On This Error]   
    
    document.getElementById(elmId).innerHTML = value;
    
    Line 75  
    

    In addition to this i wonder why title and time did not show unless I click one of these playlist pictures?