Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 855
mcan Avatar asked Dec 14 '12 20:12

mcan


People also ask

How do I fix document getElementById null?

This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag.

What is document getElementById () value?

HTML DOM Document getElementById() The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM.

What can I use instead of document getElementById?

A commonly-used alternative to document. getElementById is using a jQuery selector which you read about more here.

How do I get the document ID in getElementById?

getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.


2 Answers

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.

like image 163
Olemak Avatar answered Sep 20 '22 06:09

Olemak


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;   } } 
like image 34
bits Avatar answered Sep 22 '22 06:09

bits