yeeen
yeeen

Reputation: 4945

JavaScript createElement qn

Why doesn't this piece of code work? When I used the commented part instead of the table it works... Use table why cannot work?

<html>
    <head>
        <script type="text/javascript">
        function addLine() {
            var p = document.createElement('p');
            p.innerHTML = "hello";
            document.getElementById("addline").appendChild(p);
        }

        </script>
    </head>
    <body>
    <!--
    <input type="submit" value="New Line" onClick="addLine()"/>
    <div id="addline"></div>
    -->
        <table>
            <tbody>
                <tr>inside a table</tr>
            </tbody>
            <tfoot>
                <tr><input type="submit" value="New Task" onClick="addLine()"/></tr>
                <tr id="addline"></tr>
            </tfoot>                        
        </table>
    </body>
</html>

Upvotes: 0

Views: 217

Answers (2)

Warty
Warty

Reputation: 7405

you can only append into <td>...

Instead of:

<tr id="addline"></tr>

Use:

<tr><td id="addline"></td></tr>

For the second question, "for" is a reserved Keyword in many languages. You can't use it, and if you find a way to do so [setAttribute or something like that, not that I've tested that, i'm just throwing a wild guess] its bad programming practice.

Maybe you can use variable name "isFor" or "usage" =/ idk

Upvotes: 1

Quentin
Quentin

Reputation: 944320

You are trying to append a <p> element to <tr> element, but only <td> and <th> elements are allowed there.

Upvotes: 2

Related Questions