Skip to main content

Uncaught TypeError: Cannot read property 'addEventListener' of undefined

  • var el = document.getElementById('overlayBtn');
    if(el){
      el.addEventListener('click', swapper, false);
    }

    You can simply check variable (el) before adding an event listener to element. If element exists then it will add a click event to it. 

  • Back to code snippet queries related javascript