tags:

views:

31

answers:

3

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>
+2  A: 

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

David Dorward
Not if he uncomments the commented-out part, as he said he does when it stops working. Then it's indeterminate whether he's trying to append to a `div` or a `tr`.
T.J. Crowder
No, he said when he uses that **instead** of the table it **does** work (since a p may be a child of a div).
David Dorward
T.J. Crowder
I have days like that.
David Dorward
+1  A: 

(Answer removed, only didn't delete it to preserve the comments. My answer to Question 1 was wrong [thanks David] and you removed Question 2, so...)

T.J. Crowder
There is only one element with any given id in the code example
David Dorward
@David: He said when he uncommented the commented-out section, it stopped working. The commented-out section has `<div id="addline"></div>` which conflicts with his table row later (`<tr id="addline"></tr>`). I did say "the div and the table row".
T.J. Crowder
No, he said when he uses that **instead** of the table it **does** work (since a p may be a child of a div).
David Dorward
I don't mean that actually. I meant if i use the commented part "instead of the table"... U misunderstood my meaning.Btw, ur setAttribute works...
yeeen
@David: Sure enough. Fixed.
T.J. Crowder
setAttribute doesn't work in IE < 8 (for this case anyway).
David Dorward
How do i know if sth.sthelse can be used? Is there any online resources for this part? Don't how to search for these...
yeeen
shall i put bck qn 2 part? if not ppl may not understand what r we discussing abt...
yeeen
@yeeen: I'd do a different question on it. It's actually kind of interesting.
T.J. Crowder
Ok. When u hv done the qn, do let me hv the link, cos i want to see also.
yeeen
@yeeen: It would be for you to do the question, but there's no need -- it's been asked and answered, and separately I got curious and found the answer with a search. See my comment on your question above. Happy coding.
T.J. Crowder
+1  A: 

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

ItzWarty
It's not bad practice to use `a.setAttribute('for')`. At that point he's specifying the name of an attribute, which has nothing to do with JavaScript's reserved keywords (and which he *has* to do if he wants to set that attribute on the element).
T.J. Crowder
why <tr><td id="addline"></td></tr> works but <tr id="addline"></tr>don't?
yeeen
Then why 'label1.for = "aaa"' can't be used if it is not the case of reserve word?
yeeen
@yeeen — for the reasons I described in my answer, and because it *is* a reserved word, respectively.
David Dorward
ok, got it... cos when i read urs i don't understand, when i saw itz's, i forgot abt urs... sorry for the trouble..
yeeen
@yeeen : Basically, in HTML "tr" starts a table row. "td" starts a table cell. When you have a table, you're not writing into a row, you're writing into a cell. That is the logic you should follow. Hope that helps!
ItzWarty
Re my comment above: It's not bad practice, but it doesn't work on IE6 or IE7. You were on the right track, it's `htmlFor`: `a.htmlFor = 'name';`. So that goes on the list along with `cssFloat` and `className`. :-)
T.J. Crowder