0

HTML

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <title>Game</title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <script src="script.js"></script>   

</head>

<body>

    <div class="container">

    <table class="table ">
        <tr>
            <td id="1">a</td>           
            <td id="2">a</td>           
            <td id="3">a</td>           
            <td id="4">a</td>
        </tr>
        <tr>
            <td id="5">a</td>           
            <td id="6">a</td>           
            <td id="7">a</td>           
            <td id="8">a</td>
        </tr>
        <tr>
            <td id="9">a</td>           
            <td id="10">a</td>          
            <td id="11">a</td>          
            <td id="12">a</td>
        </tr>
        <tr>
            <td id="13">a</td>          
            <td id="14">a</td>          
            <td id="15">a</td>          
            <td id="16">a</td>
        </tr>
    </table>

    </div>


</body>

function startFunction() {
    var table = document.querySelector('#table');
    var table_cells = table.querySelectorAll('td');   
}


window.onload = function() {
  startFunction();
};

I keep getting an error of "uncaught typeError: cannot read property 'querySelectorAll' of null."

I think this has to do with the fact when I'm calling a function that tries to grab said element node before it exists perhaps?

I'm loading my Javascript file in the <head> section of my HTML file so I added a window.onload function to make sure everything is loaded before the page runs. However, I still keep getting this error.

Iván Rodríguez Torres
  • 3,733
  • 3
  • 26
  • 40
Faique Moqeet
  • 549
  • 1
  • 4
  • 9
  • 1
    Could you add the accompanying HTML markup to your question please? – Cᴏʀʏ Jan 19 '17 at 18:52
  • 1
    Your code should work fine. Are you sure your table has `id="table"`? – Tyler Roper Jan 19 '17 at 18:53
  • @Santi Just noticed I had "table" as a class. Fixed that. – Faique Moqeet Jan 19 '17 at 18:54
  • Santi's answer is correct. An additional improvement would be to use `document.addEventListener('DOMContentLoaded', function() { ... });` instead of `window.onload = ...`. Here's a good question and answer on the topic: http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery – Jordan Running Jan 19 '17 at 19:00
  • @Jordan Why is that better than using window.onload = ... ? – Faique Moqeet Jan 19 '17 at 19:05
  • 1
    From the linked question: "`window.onload` fires after all images, frames, etc. have been loaded." Usually you only want to wait until the DOM has finished loading, ergo DOMContentLoaded. – Jordan Running Jan 19 '17 at 19:09
  • Makes sense, thanks. – Faique Moqeet Jan 19 '17 at 19:10
  • 1
    And, as the first paragraph of the [DOMContentLoaded docs](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded) on MDN reads: "The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event `load` should be used only to detect a fully-loaded page. It is an incredibly popular mistake to use `load` where DOMContentLoaded would be much more appropriate, so be cautious." – Jordan Running Jan 19 '17 at 19:11

2 Answers2

3

Take a look at this line in your JavaScript:

var table = document.querySelector('#table');

The pound-sign indicates an ID, and so your javascript is looking for an element with the ID of "table".

But, if you look at your table, it doesn't have an ID; only class.

The solution is to give your <table> an ID of "table" so that the javascript properly finds it:

<table id="table">
Tyler Roper
  • 20,529
  • 6
  • 30
  • 51
1

The table is missing id attribute. Instead, it has class attribute. Fix by adding id

<table class="table" id="table">

Or modify your query selector to select by class:

var table = document.querySelector('.table');
Adam Azad
  • 10,675
  • 5
  • 25
  • 63