Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Table innerHTML making a <tbody>?

    Hi, I am using greasemonkey to edit a page, and the site does not use <tbody> within its tables.

    This is the table I want to change:
    Code:
    <table><tr><td><form ... ><input ... ><input ... ></form></td></tr></table>
    When I checked the innerHTML of one of the tables, it gave me:
    Code:
    <table><tbody><td><tr><form ... ></form><input ... ><input ... ></td></tr></tbody></table>
    There are two things wrong: I never put in a <tbody>, and it closes the form right when the tag is opened.

    Is there any way to get JUST what is within the HTML, without generating (incorrect) code?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    innerHTML reports the html after it is read by the browser.

    innerHTML is almost never the same as the literal text, and it is not likely to be the same in two different browsers.

    IE reads html tags and many attributes in uppercase, and it will leave out quotation marks in some of your attributes, and an occasional closing tag.
    If the browser does not read a tag like tbody, that is 'optional' in the html dtd it will insert it where it expects it to be.

    So which is more important to know- the code you wrote, or the code the browser is using to render the page?

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by itsjareds View Post
    Hi, I am using greasemonkey to edit a page, and the site does not use <tbody> within its tables.

    This is the table I want to change:
    There are two things wrong: I never put in a <tbody>, and it closes the form right when the tag is opened.

    Is there any way to get JUST what is within the HTML, without generating (incorrect) code?
    On the contrary, this is the correct generated code. Even if in HTML tbody is not required, in Javascript it is, thus any browser in this world will show you the TBODY element as present in a TABLE, no matter you have physically written it or not. Further more, if you want to use DOM methods to create/remove table's rows, you must consider the tbody as present and take that tbody as the parent of the rows.
    .
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,387
    Thanks
    32
    Thanked 288 Times in 282 Posts
    Quote Originally Posted by itsjareds View Post
    There are two things wrong: I never put in a <tbody>, and it closes the form right when the tag is opened.
    The tbody element has optional start and end tags. Elements like that are essentially implied and inserted into the DOM automatically by the browser when needed. Essentially, all table elements must contain a tbody element unless all of its tr elements are contained within thead and tfoot elements; the latter situation would almost certainly be an incorrect use of an HTML table, so, effectively, that means that all correctly written HTML tables have at least one tbody element.

    If it helps, consider that tr elements are invalid when placed as children of table elements. Only col, colgroup, del, ins, tbody, tfoot, and thead elements may be children of a table element. The following DOM representation is incorrect per the HTML 4.01 DTD. (The code is valid source code but not correct output.)

    Code:
    <table>
    	<tr>
    		<td></td>
    	</tr>
    </table>
    The browser fixes it by inserting implied tags.

    Code:
    <table>
    	<tbody><tr>
    		<td></td>
    	</tr>
    </tbody></table>
    You have the exact same situation with other implied tags as shown in the following example (comprised of correctly written source code).

    Code:
    <!doctype html public "-//W3C//DTD HTML 4.01//EN">
    <title></title>
    <p><div></div>
    <p><var></var>
    </body>
    The resulting code would be the following.

    Code:
    <!doctype html public "-//W3C//DTD HTML 4.01//EN">
    <html><head><title></title>
    </head><body><p></p><div></div>
    <p><var></var>
    </p></body></html>
    For every complex problem, there is an answer that is clear, simple, and wrong.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •