[Django]-TypeError: document.getElementById(…) is null Django + JS

3👍

Your javascript file is running before the button exists in the DOM, so it can not find it

wrapper your code with use window.onload

whenever you access a DOM element, it must already be rendered

0👍

Perform a null checker

let button = document.getElementById('id');

if(typeof button !== 'undefined' && button !== null) {
  button.onclick = () => { /* Do your stuff */ };
}

0👍

Button’s type="submit" must be eliminated if you want to add listener.

👤Thevs

Leave a comment