0

Here is an error:

Uncaught TypeError: Cannot read property 'appendChild' of null

The example of code:

function MakeTable(Matrix){
    var newElem = document.createElement('table');
    newElem.border = "1px";
    for(var j = 0; j < Matrix.length; j++) {
        var newRow = newElem.insertRow(j);
        for(var n = 0; n < Matrix[j].length; n++) {
            var newCell = newRow.insertCell(n);
            newCell.innerText = Matrix[j][n];
        }
    }

    document.getElementById('table1').appendChild(newElem);
}
4
  • Seems pretty clear that your table1 element isn't found in the DOM. Commented May 23, 2015 at 17:53
  • <body> <div> <h1>Дана матриця:</h1> <p> 1-A&nbsp;&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;-1 </p> <p> &nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;-1&nbsp;&nbsp;1&nbsp;&nbsp;-1 </p> <p> &nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;-1&nbsp;&nbsp;0 &nbsp;&nbsp;0 </p> <p> &nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 0&nbsp;&nbsp;1 </p> Де А = 10^K, K = 12; </div> <div id="table1"> </div> </body> Commented May 23, 2015 at 17:55
  • If the script is in the <head> and it isn't using a delay to make sure it doesn't run when until the DOM is loaded, then it'll run before the element exists. Move your script to just before the closing </body> tag. Commented May 23, 2015 at 17:57
  • What is the param Matrix you are passing through to MakeTable()? An object, array etc? Commented May 23, 2015 at 17:58

3 Answers 3

1

As long as you have an element with the table1 ID in the DOM and as long as the script doesn't run before that element is loaded, it will work.

function MakeTable(Matrix){
    var newElem = document.createElement('table');
    newElem.border = "1px";
    for(var j = 0; j < Matrix.length; j++) {
        var newRow = newElem.insertRow(j);
        for(var n = 0; n < Matrix[j].length; n++) {
            var newCell = newRow.insertCell(n);
            newCell.textContent = Matrix[j][n];
        }
    }

    document.getElementById('table1').appendChild(newElem);
}

MakeTable([["this", "is"], ["my", "table"]]);
<div id=table1></div>

I also changed .innerText to .textContent since that's the standards compliant way to set text.

Sign up to request clarification or add additional context in comments.

Comments

1

If you create an element, you have to append it. Here:

document.body.appendChild(table)

Change the last line:

function MakeTable(Matrix){
    var newElem = document.createElement('table');
    newElem.border = "1px";
    for(var j = 0; j < Matrix.length; j++) {
        var newRow = newElem.insertRow(j);
        for(var n = 0; n < Matrix[j].length; n++) {
            var newCell = newRow.insertCell(n);
            newCell.innerText = Matrix[j][n];
        }
    }
    document.body.appendChild(newElem);
}

By convention you write the first letter of a function name in lower case (except class name).

Demo

Comments

0

It returned out that I linked script not in the body, but in the head)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.