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

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?

This question is related to javascript dom runtime-error innerhtml getelementbyid

The answer is


In your code, you can find this function:

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

Later on, you call this function with several params:

updateHTML("videoCurrentTime", secondsToHms(ytplayer.getCurrentTime())+' /');
updateHTML("videoDuration", secondsToHms(ytplayer.getDuration()));
updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
updateHTML("startBytes", ytplayer.getVideoStartBytes());
updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
updateHTML("volume", ytplayer.getVolume());

The first param is used for the "getElementById", but the elements with ID "bytesTotal", "startBytes", "bytesLoaded" and "volume" don't exist. You'll need to create them, since they'll return null.


Found similar problem within student's work, script element was put after closing body tag, so, obviously, JavaScript could not find any HTML element.

But, there was one more serious error: there was a reference to an external javascript file with some code, which removed all contents of a certain HTML element before inserting new content. After commenting out this reference, everything worked properly.

So, sometimes the error might be that some previously called Javascript changed content or even DOM, so calling for instance getElementById later doesn't make sense, since that element was removed.


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.


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

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


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;
    }
);

I have same problem. It just the javascript's script loads too fast--before the HTML's element loaded. So the browser returning null, since the browser can't find where is the element you like to manipulate.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to dom

How do you set the document title in React? How to find if element with specific id exists or not Cannot read property 'style' of undefined -- Uncaught Type Error adding text to an existing text element in javascript via DOM Violation Long running JavaScript task took xx ms How to get `DOM Element` in Angular 2? Angular2, what is the correct way to disable an anchor element? React.js: How to append a component on click? Detect click outside React component DOM element to corresponding vue.js component

Examples related to runtime-error

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined' what does Error "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" mean? javac: invalid target release: 1.8 C++ error : terminate called after throwing an instance of 'std::bad_alloc' Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Error No Main class found in NetBeans Runtime error: Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0 TypeError: Cannot read property "0" from undefined Array Index Out of Bounds Exception (Java) Excel VBA Automation Error: The object invoked has disconnected from its clients

Examples related to innerhtml

React.js: Set innerHTML vs dangerouslySetInnerHTML Angular 2 - innerHTML styling How do I clear inner HTML Show/hide 'div' using JavaScript How to get the innerHTML of selectable jquery element? Cannot set property 'innerHTML' of null Add/remove HTML inside div using JavaScript Javascript - Replace html using innerHTML Add inline style using Javascript It says that TypeError: document.getElementById(...) is null

Examples related to getelementbyid

getElementById in React How to access a DOM element in React? What is the equilvalent of document.getElementById() in React vue.js 'document.getElementById' shorthand Cannot read property 'addEventListener' of null Onclick function based on element id Use of document.getElementById in JavaScript How to pick element inside iframe using document.getElementById It says that TypeError: document.getElementById(...) is null document.getElementById('btnid').disabled is not working in firefox and chrome Change the Value of h1 Element within a Form with JavaScript