Js script not connecting to html?

  • 0
    In JSFiddle it works but when running html file from desktop the script doesn't work
    <head>
      <meta charset="utf-8">
      <title></title>
      <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;
        };
      </script>  
    </head>
    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.
    Anonymous

  • 0
    includeHTML will help.
    Anonymous

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

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