Fabian Falck Fabian Falck - 2 months ago 5x
Javascript Question

getElementById(...) returns null unexpectedly

EDITED: I pretty much checked dozens of articles here on this issue, but I simply can't find the problem in my code: The line (commented below) returns null, although I expect it to return the particular element I created and added above.

element2 = document.createElement("button");
element2.innerHTML = "-";
element2.text = rowCounter;
element2.style.backgroundColor = 'white';
element2.id = "kuerzer"+rowCounter; //rowCounter is an iterator variable
console.log(document.getElementById(element2.id)); //returns null

However, I am now trying to find this element in another function like this:

function structureGantData(){
var gantData = [[]];
for(i=0; i<document.getElementById("myTable").rows.length; i++){
gantData[i][0] = document.getElementById("kuerzer",(i+1)).text; //ISSUE

Could anyone help me? :-)

Thanks alot!



I believe you meant to call document.getElementById("kuerzer" + (i + 1)) rather than document.getElementById("kuerzer", i + 1) in structureGantData.