Simple jQuery click not working

Dave Rottino picture Dave Rottino · Jan 3, 2012 · Viewed 34.9k times · Source

What are the possible reasons for document.getElementById, $("#id") or any other DOM method / jQuery selector not finding the elements?

Example problems include:

  • jQuery silently failing to bind an event handler
  • jQuery "getter" methods (.val(), .html(), .text()) returning undefined
  • A standard DOM method returning null resulting in any of several errors:

Uncaught TypeError: Cannot set property '...' of null Uncaught TypeError: Cannot read property '...' of null

The most common forms are:

Uncaught TypeError: Cannot set property 'onclick' of null

Uncaught TypeError: Cannot read property 'addEventListener' of null

Uncaught TypeError: Cannot read property 'style' of null

Answer