0

Just testing this out, trying to insert a table into the body tag but I can only do it with getElementById. I also tried inserting the script code at the bottom, but that doesn't work either.

<html>

<head>
        <title>Exam Test Trial</title>
        <script>
        var table;
            var books = {1234: "Programming for Dummies", 5667: "Ethical Hacking", 88889: "Networks", 10000: "Firmware Code"};

                function createTable(){
                table = "<table border= '1'>";

                table += "<tr>";

                for(var isbn in books){
                    table += "<th>";
                    table += isbn;
                    table += "</th>";
                }

                table += "</tr>";
                table += "</table>";


                //var x = document.getElementById("exam").innerHTML = table;

                var y;
            }


        </script>
</head>

<body id="exam">
    <script>
        createTable();
        var x = document.getElementsByTagName("body").innerHTML = table;
    </script>
</body>

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jon Q
  • 63
  • 1
  • 1
  • 7

1 Answers1

2

getElementsByTagName("body") returns an array-like structure. Did you mean getElementsByTagName("body")[0], perhaps?

spender
  • 117,338
  • 33
  • 229
  • 351
  • 2
    @Havvy Got a reference to that first one? As far as I can tell, it doesn't exist – Phil Dec 09 '14 at 01:06
  • 1
    @Havvy Is there `getElementByTagName`? I'm not sure that exists. – spender Dec 09 '14 at 01:06
  • This is the proper answer, he was not aware it was an array. – Tai Kwangi Chicken Dec 09 '14 at 01:07
  • @GwiddleWorker—[*getElementsByTagName*](http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-A6C9094) doesn't return an Array, it returns a [*NodeList*](http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-536297177). ;-) – RobG Dec 09 '14 at 01:54