1

I have a webpage with less and prefixfree (both of them are js-files, imported from my own server) and now, when I've tried to import a custom script too, I got the problem:

Basic javascript like

x = "hello world";
alert (x);

works fine.

But every code which refers to my document like

alert (document.getElementById("content").innerHTML);

gives the console error

"document.getElementById("content").innerHTML is undefined"

Do you know what the problem could be? Any suggestions how to debug this are also welcome - I'm just despairing!

edit

the JS-files are all imported in the <head>.

James A Mohler
  • 10,562
  • 14
  • 41
  • 65
Jere
  • 915
  • 1
  • 7
  • 23
  • possible duplicate of [Why does jQuery or a DOM method such as \`getElementById\` not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Felix Kling Apr 16 '14 at 15:32
  • Are you sure to have an element with an id attribute **id="content"** dude ? – user3241019 Apr 16 '14 at 15:32
  • How and where in the page is the code being executed? – j08691 Apr 16 '14 at 15:33
  • @user3241019 I'm sure. I've tried that also with different ids, with docuemnt.getElementByClassName or document.getElementByTagName. Always the same error message – Jere Apr 16 '14 at 15:36

1 Answers1

5

Make sure your javascript is after your markup and make sure the element id is correct:

<div id="content">Beep boop</div>
<script>
    alert(document.getElementById("content").innerHTML);
</script>

Example: http://jsbin.com/sedagite/2/edit

Chip
  • 241
  • 1
  • 4