Oned Zair
Oned Zair

Reputation: 33

Cannot read property Length of InnerHTML and div id

This is the text to be checked:

<div id="List1" style="display:none">
   VIDEO 1</br>
   VIDEO 2</br>
   VIDEO 3</br>
</div>

This is the Javascript code:

var div = document.getElementById("List"+id); //id is a random number

if(div.innerHTML != "") // <-- this gives an error.
{
    document.getElementById("List"+id).style.display = "block";
}

When the debugger reads: if (div.innerHTML != ""), it gives this error: Uncaught TypeError: Cannot read property 'Length' of undefined.

I want to check if one of the List is empty/not set/not existing.

Upvotes: 0

Views: 1766

Answers (3)

KarSho
KarSho

Reputation: 5746

@Oned Zair, by following,

var myElem = document.getElementById('myElementId');
if (myElem == null) alert('does not exist!');

or

$(id).length > 0 

you can check div exist or not.

Upvotes: 4

THE ONLY ONE
THE ONLY ONE

Reputation: 2190

There is wrong syntax of innerHTML please change it to this code,....

if(document.getElementById("List"+id).innerHTML != "") { document.getElementById("List"+id).style.display = "block"; }

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

The i in innerHTML needs to be lowercase. Change InnerHTML to innerHTML . It would also be prudent to check that the div exists in the conditional.

if(div && div.innerHTML != "")
{
    document.getElementById("List"+id).style.display = "block";
}

Upvotes: 2

Related Questions