function createTable(tableData) { var table = document.createElement('table'); var tableBody = document.createElement('tbody'); if(document.querySelectorAll('._Table').length > 0){ for(var i = document.querySelectorAll('._Table').length - 1; i >= 0; i--){ document.querySelectorAll('._Table')[i].remove(); }; }; table.classList.add('_Table'); tableData.forEach(function(rowData,i) { var row = document.createElement('tr'); rowData.forEach(function(cellData,j) { var cell = document.createElement('td'); cell.id=i+'_'+j cell.appendChild(document.createTextNode(cellData)); row.appendChild(cell); }); tableBody.appendChild(row); }); table.appendChild(tableBody); document.body.appendChild(table); } document.querySelector(".btn").addEventListener("click", function() { var row = document.querySelector(".col1").value; var col = document.querySelector(".col2").value; var row_mas = []; var col_mas = []; for (var i = 0; i < row; i++) { var col_mas = []; for (var j = 0; j < col; j++) { col_mas.push(0); } row_mas.push(col_mas); } createTable(row_mas); }); document.querySelector(".btn2").addEventListener("click", function() { var row = document.querySelector(".col3").value; var col = document.querySelector(".col4").value; var gell = document.getElementById(row+"_"+col); gell.innerHTML="1" }); 
 body { margin-top: 10%; margin-left: 40%; } th,td { width: 50px; border: 1px solid red; font-size: x-large; color: white; } tbody { background-color: blue; } input { width: 20px; height: 20px; } 
 <input type="text" class="col1"> <input type="text" class="col2"> <button class="btn">Go</button> <br/> <input type="text" class="col3"> <input type="text" class="col4"> <button class="btn2">koordinats</button> 

Closed due to the fact that off-topic by the participants Kromster , Denis Bubnov , user194374, ߊߚߤߘ , Grundy 7 Feb '17 at 14:28 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Denis Bubnov, Community Spirit, Grundy
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Questions asking for help with debugging (“why does this code not work?”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question. Questions without an explicit description of the problem are useless for other visitors - Kromster
  • In this snippet, everything works, with manual initialization and with manual checking of boundaries .. - vp_arth

1 answer 1

The error is obvious:

Cannot set property 'innerHTML' of null "

This means that the result of calling document.getElementById(row+"_"+col) -> null

  • and why between row + + col is the underscore "_"? - Anton
  • @Anton, is this your code? :-) don't you remember why you wrote _ ? Here, a string is simply assembled, which must match the id of the element to be found, for example: 1_1 or 2_2 and the underscore is simply divided into parts, so that visually by markup one can tell which rows / columns the element belongs to - Grundy