-1

getElementById is returning null when assigned to a variable.

var element = document.getElementById("ul1");
var newelement = document.createElement("li");
newelement.innerHTML = "New";
element.appendChild(newelement);
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
Vijay Thapa
  • 51
  • 1
  • 5

1 Answers1

0

When you put your javascript in the head of the document it will load (and block the loading of the body) before any other element on the page.

A quick example of what you're actually doing would be this:

<!-- script runs before element creation -->
<script type="text/javascript">
    var element = document.getElementById('ul1');
</script>
<!-- this is after execution, where JS could not find the element but only now is the element actually created -->
<ul id="ul1"></ul>

To solve this issue, you can do a few things

  • place the <script> tag below the element you want to target
  • place the <script> tag just above the closing </body> tag)
  • use window.onload = function() { ... } to make sure the page is loaded before doing anything.

Then the ul element will exist and you will be able to append your LI :)

SidOfc
  • 4,552
  • 3
  • 27
  • 50
  • In a comment to Mathews' answer you said: "it's about the DOM not being loaded" How can you know that? OP's code snippet doesn't provide that information. – Teemu Dec 08 '15 at 17:12
  • My personal favorite is to put my script(s) in a (few) function(s) in a linked file, then just call them right above `

    `. It's cleaner and takes advantage of caching.

    – abluejelly Dec 08 '15 at 17:23
  • @Teemu because the only reasons `document.getElementById` can't find your element is if either 1)`The ID literally does not exist (typo'd somwhere or removed)` or 2)`DOM isn't loaded`. The former is a very basic debug problem, the latter is simply not knowing that's a thing. – abluejelly Dec 08 '15 at 17:26
  • @abluejelly Nope, there is [one more reason](http://jsfiddle.net/ob5Lq47q/) : ). – Teemu Dec 08 '15 at 17:27
  • @Teemu that would be the 1 option- literally does not exist. You're trying to be a smartass, and you're not doing a very good job of it :^) – abluejelly Dec 08 '15 at 17:31
  • @abluejelly You didn't took a careful look at the code. The element exists, but it's not appended to the DOM, hence DOM getters can't find it (`querySelector` could find it). Looking at OP's code, it's a possible case. What comes to smartasses, well, you know ... – Teemu Dec 08 '15 at 17:33
  • @Teemu except it doesn't exist. `document.createElement` does not append to the `document`, it's merely a constructor. Thus, the element still does not exist inside `document`, which is where `document.getElementById` naturally searches. We're still in the "literally does not exist" state, and obviously so since there isn't an `element.createElement` function to make you think it'll auto-append it as a child of `document`. I stand by you're trying to be a smartass, and not succeeding. – abluejelly Dec 08 '15 at 17:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97310/discussion-between-abluejelly-and-teemu). – abluejelly Dec 08 '15 at 17:38