Js script not connecting to html?

  • 0
    In JSFiddle it works but when running html file from desktop the script doesn't work
      <meta charset="utf-8">
      <script type="text/javascript">
        document.querySelector('table').onclick = function(e) {
            let target = e.target;
            if (target.tagName != 'BUTTON') return;
            let cnt = target.closest('td').querySelector('.cnt');
            cnt.innerText = parseInt(cnt.innerText, 10)+1;
    JavaScript Anonymous, May 2, 2020

  • 3 Answers
  • 0
    The script in the header is executed immediately and at the time of its execution there is no table element in the DOM yet.

    Move the script to the end of the file or use the DOMContentLoaded event.

  • 0
    includeHTML will help.

  • 0
    Try to stick the script at the end of the file

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!