Search code examples
javascriptgetelementbyid

getElementById returns null?


document.getElementById('id of div that definately exists') returns null.

I originally loaded the javascript last in order to make sure I wouldn't need to worry about the onload event. I also tried using the onload event. It's very spooky. Any ideas or help would be greatly appreciated.


Solution

  • It can be caused by:

    1. Invalid HTML syntax (some tag is not closed or similar error)
    2. Duplicate IDs - there are two HTML DOM elements with the same ID
    3. Maybe element you are trying to get by ID is created dynamically (loaded by ajax or created by script)?

    Please, post your code.