Search code examples
javascriptexceptiondomappendchild

Something about this 'NOT_FOUND_ERR: Dom exception 8' confuses me


Well, here I continue, trying to learn this very nice language... So I previously had a very ugly code full of 'document.write()' and more ugly things, and now I am transforming it into a very nice standards compliant code, and I am liking it! But I found a problem where I don't see the logic. Here it goes:

In the html file I have this:

<body onload="generatetable(0)">

And in the .js file I have this:

function generatetable(product) {    
    var tbinfo = document.createElement("table");  //table
    var tbbody = document.createElement("tbody");  //tbody
    var row = document.createElement("tr");     // creates row
    for (var i = 0; i < 4; i++) {               // creates 4 cells
        var cell = document.createElement("td");
        }

    var tname = arraypro[product].Name;
    cell.appendChild(tname);

(I don't paste the rest of the table, because it seems to work fine)

And when running, I get the 'Exception 8' error on the var tname = arraypro[product].Name line

But if I do just an

alert(arraypro[0].Name);

it outputs exactly what it should output, the very right word. How it's possible that the value in arraypro[product].Name can be retrieved by an alert (if you pass the value of 'product') but not by the appendChild?

I am still not very well used to the logic of programming, but I try...

PD: The arraypro , where the information is, has been declared as a global value, accessible for everything, in case it matters to know.

Thanks very much for any input here.


Solution

  • appendChild() expects a DOM-Node, you're passing in a string I believe.

    You can change it to:

    var tname = arraypro[product].Name;
    cell.appendChild(document.createTextNode(tname));