create a new row in a table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Kirk

    #1

    create a new row in a table

    Hi there

    can someone please help me with creating dynamic content in a table? For
    example, see the below javascript and html - why is a new row not created in
    the table when I click the button? (I am using Internet Explorer 6).

    <html>
    <head>
    <title>TEST</title>
    <script language="javas cript">
    function addField() {
    alert( 'Add Field' );
    my_div.innerHTM L = my_div.innerHTM L + "<tr><td>ne w row</td></tr>";
    }
    </script>
    </head>

    <body>
    <table border="1">
    <tr>
    <td>
    first row
    </td>
    </tr>

    <div id="my_div"></div>

    <tr>
    <td>
    <input type="button" value="click me"
    onclick="javasc ript:addField() " >
    </td>
    </tr>
    </table>
    </body>
    </html>

    Thanks,
    Peter

  • kaeli

    #2
    Re: create a new row in a table

    In article <4157cf8c$1@new s.wineasy.se>, "Peter Kirk" <peter> enlightened us
    with...[color=blue]
    > Hi there
    >
    > can someone please help me with creating dynamic content in a table? For
    > example, see the below javascript and html - why is a new row not created in
    > the table when I click the button? (I am using Internet Explorer 6).
    >[/color]

    Invalid HTML.

    This is a horrible way to make dynamic tables, too.
    [color=blue]
    > </tr>
    >
    > <div id="my_div"></div>[/color]

    A div is not allowed to be the child of TBODY.
    TBODY has only TR as a child.
    http://www.w3.org/TR/REC-html40/stru...tml#edef-TBODY
    <!ELEMENT TBODY O O (TR)+ -- table body -->
    [color=blue]
    >
    > <tr>
    > <td>
    > <input type="button" value="click me"
    > onclick="javasc ript:addField() " >[/color]

    onClick is already script. Drop the "javascript :" stuff from it.
    onClick="addFie ld()"

    To make dynamic table rows, use createElement and appendChild.
    Create the element, then append it to TBODY. Or use of the insert methods or
    replace if you'd prefer that.

    --
    --
    ~kaeli~
    The definition of a will?... (It's a dead giveaway.)



    Comment

    • Peter

      #3
      Re: create a new row in a table

      Hi, thanks for the response...

      "kaeli" <tiny_one@NOSPA M.comcast.net> skrev i en meddelelse
      news:MPG.1bc1d8 993ae2ed8798a17 9@nntp.lucent.c om...[color=blue]
      > In article <4157cf8c$1@new s.wineasy.se>, "Peter Kirk" <peter> enlightened[/color]
      us[color=blue][color=green]
      > > can someone please help me with creating dynamic content in a table? For
      > > example, see the below javascript and html - why is a new row not[/color][/color]
      created in[color=blue][color=green]
      > > the table when I click the button? (I am using Internet Explorer 6).
      > >[/color]
      >
      > Invalid HTML.
      >
      > This is a horrible way to make dynamic tables, too.[/color]

      Quite likely.... I know zip about javascript, and almost zip about html....
      [color=blue][color=green]
      > > </tr>
      > >
      > > <div id="my_div"></div>[/color]
      >
      > A div is not allowed to be the child of TBODY.[/color]

      OK - that's what you mean by it being invalid - a DIV is not allowed inside
      a TABLE? Don't really know what a "DIV" is, just thought it was a way to
      create a label you could reference from other places in the html.

      [color=blue]
      > TBODY has only TR as a child.
      > http://www.w3.org/TR/REC-html40/stru...tml#edef-TBODY
      > <!ELEMENT TBODY O O (TR)+ -- table body -->[/color]

      What is "TBODY" actually. Is it like TABLE?

      [color=blue][color=green]
      > >
      > > <tr>
      > > <td>
      > > <input type="button" value="click me"
      > > onclick="javasc ript:addField() " >[/color]
      >
      > onClick is already script. Drop the "javascript :" stuff from it.
      > onClick="addFie ld()"
      >
      > To make dynamic table rows, use createElement and appendChild.
      > Create the element, then append it to TBODY. Or use of the insert methods[/color]
      or[color=blue]
      > replace if you'd prefer that.[/color]

      OK thanks. I've got a place to start looking now.


      Comment

      • kaeli

        #4
        OT: proper HTML (was Re: create a new row in a table)

        In article <41584f67@news. wineasy.se>, peter@ciber enlightened us with...[color=blue]
        >
        > Quite likely.... I know zip about javascript, and almost zip about html....
        >[/color]

        Learn HTML first. You'll never code decent DHTML until you do.
        [color=blue][color=green][color=darkred]
        > > > </tr>
        > > >
        > > > <div id="my_div"></div>[/color]
        > >
        > > A div is not allowed to be the child of TBODY.[/color]
        >
        > OK - that's what you mean by it being invalid - a DIV is not allowed inside
        > a TABLE? Don't really know what a "DIV" is, just thought it was a way to
        > create a label you could reference from other places in the html.
        >[/color]

        No, HTML is a structured layout language. It has elements. Elements have
        attributes.
        Documents have structure, and that structure is pre-determined to a point.
        A DIV is a block-level element. As is P. SPAN is NOT a block-level element.
        A table is a table, and as such, much have a certain structure. TBODY is part
        of that and is implied if not explicitly stated.

        This, in a nutshell, is the structure. (lots more elements are allowed, but
        this is sufficient for now)

        TABLE
        -THEAD
        -TBODY
        -TR
        -TD
        -anything you want, including DIVS
        -TFOOT

        So, what you were trying to do in your code was append a table row to a div.
        That's not allowed. A table row can only be the child element of a table
        head, body, or foot.
        [color=blue]
        >[color=green]
        > > TBODY has only TR as a child.
        > > http://www.w3.org/TR/REC-html40/stru...tml#edef-TBODY
        > > <!ELEMENT TBODY O O (TR)+ -- table body -->[/color]
        >
        > What is "TBODY" actually. Is it like TABLE?[/color]

        No, it's the body of the table.
        THEAD is the header.
        TFOOT is the footer.
        [color=blue]
        >
        > OK thanks. I've got a place to start looking now.[/color]

        Actually, you should really learn a bit more about html first. You need to
        know what valid elements are and where to put them before you start creating
        new ones. :)

        A good place for you to start is to ask for good tutorials over in
        comp.infosystem s.www.authoring.html. Do read their FAQ first. They're kinda
        picky about that over there.

        And learn about doctypes while you're there.

        For your perusal, here's a simple version that works and is valid html. Note
        that form elements outside a form will not submit (your button), but since
        this is just for grins, it's okay. Also note that a production version would
        test for objects and methods before using them.

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
        "http://www.w3.org/TR/REC-html40/loose.dtd">
        <html>
        <head>
        <title>TEST</title>
        <script type="text/javascript">
        function addRow() {
        var tbody = document.getEle mentById("t1");
        var newTr = document.create Element("TR");
        var newTd = document.create Element("TD");
        var newTxt = document.create TextNode("new row!");
        newTd.appendChi ld(newTxt);
        newTr.appendChi ld(newTd);
        tbody.appendChi ld(newTr);
        }
        </script>
        </head>

        <body>
        <table border="1">
        <tbody id="t1">
        <tr>
        <td>
        first row
        </td>
        </tr>
        <tr>
        <td>
        <input type="button" value="click me"
        onclick="addRow ()" >
        </td>
        </tr>
        </tbody>
        </table>
        </body>
        </html>

        --
        --
        ~kaeli~
        Acupuncture is a jab well done.



        Comment

        • Michael Winter

          #5
          Re: OT: proper HTML (was Re: create a new row in a table)

          On Mon, 27 Sep 2004 14:02:42 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
          wrote:

          [snip]
          [color=blue]
          > TABLE
          > -THEAD
          > -TBODY
          > -TR
          > -TD
          > -anything you want, including DIVS
          > -TFOOT[/color]

          That isn't quite correct. If a table contains a TFOOT element, it must
          appear before the first TBODY. This is so that in the case of a large
          table, the footer can be rendered before all of the table body data has
          been received. It would also be wise to mention that if a THEAD or TFOOT
          element is present, the table must contain at least one explicitly
          included TBODY.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • mark4asp

            #6
            Re: create a new row in a table

            On 27 Sep 2004 19:35:35 +0200, "Peter" <peter@ciber> wrote:
            [color=blue]
            >Hi, thanks for the response...
            >
            >"kaeli" <tiny_one@NOSPA M.comcast.net> skrev i en meddelelse
            >news:MPG.1bc1d 8993ae2ed8798a1 79@nntp.lucent. com...[color=green]
            >> In article <4157cf8c$1@new s.wineasy.se>, "Peter Kirk" <peter> enlightened[/color]
            >us[color=green][color=darkred]
            >> > can someone please help me with creating dynamic content in a table? For
            >> > example, see the below javascript and html - why is a new row not[/color][/color]
            >created in[color=green][color=darkred]
            >> > the table when I click the button? (I am using Internet Explorer 6).
            >> >[/color]
            >>
            >> Invalid HTML.
            >>
            >> This is a horrible way to make dynamic tables, too.[/color]
            >
            >Quite likely.... I know zip about javascript, and almost zip about html....
            >[color=green][color=darkred]
            >> > </tr>
            >> >
            >> > <div id="my_div"></div>[/color]
            >>
            >> A div is not allowed to be the child of TBODY.[/color]
            >
            >OK - that's what you mean by it being invalid - a DIV is not allowed inside
            >a TABLE? Don't really know what a "DIV" is, just thought it was a way to
            >create a label you could reference from other places in the html.[/color]

            In general I think you're better off using SPAN as a container but I
            don't even think you can do that inside a table. You wouldn't want to
            stick things between table, tbody, tr, td tags - it confuses the DOM -
            a TBODY's natural 'child' is a TR.

            If you want to have several TRs together you can put them in TBODYs.

            kaeli's advice looks good.
            [color=blue][color=green]
            >> TBODY has only TR as a child.
            >> http://www.w3.org/TR/REC-html40/stru...tml#edef-TBODY
            >> <!ELEMENT TBODY O O (TR)+ -- table body -->[/color]
            >
            >What is "TBODY" actually. Is it like TABLE?[/color]

            It's a group of TRs. You can have several TBODYs inside a TABLE. Very
            useful when you want to show, hide, create, remove bits of a TABLE,
            but don't want to do this by having these bits as separate TABLEs
            because you need to keep the column widths the same for nice tidy,
            rendering.
            [color=blue][color=green][color=darkred]
            >> > <tr>
            >> > <td>
            >> > <input type="button" value="click me"
            >> > onclick="javasc ript:addField() " >[/color]
            >>
            >> onClick is already script. Drop the "javascript :" stuff from it.
            >> onClick="addFie ld()"
            >>
            >> To make dynamic table rows, use createElement and appendChild.
            >> Create the element, then append it to TBODY. Or use of the insert methods[/color]
            >or[color=green]
            >> replace if you'd prefer that.[/color]
            >
            >OK thanks. I've got a place to start looking now.
            >[/color]

            Comment

            • kaeli

              #7
              Re: OT: proper HTML (was Re: create a new row in a table)

              In article <opsez6hwiix13k vk@atlantis>, M.Winter@blueyo nder.co.invalid
              enlightened us with...[color=blue]
              > On Mon, 27 Sep 2004 14:02:42 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
              > wrote:
              >
              > [snip]
              >[color=green]
              > > TABLE
              > > -THEAD
              > > -TBODY
              > > -TR
              > > -TD
              > > -anything you want, including DIVS
              > > -TFOOT[/color]
              >
              > That isn't quite correct. If a table contains a TFOOT element, it must
              > appear before the first TBODY.[/color]

              Please clarify "must".

              I have used tfoot and placed it after the tbody with no problems in my
              intranet code. Meaning that the table rendered as desired and expected in NN7
              and IE6.
              Will it fail on some browsers?
              Will it not validate with W3C?

              TIA
              --
              --
              ~kaeli~
              Going to church doesn't make you a Christian any more than
              standing in a garage makes you a car.



              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: OT: proper HTML

                kaeli <tiny_one@NOSPA M.comcast.net> writes:
                [color=blue][color=green]
                >> That isn't quite correct. If a table contains a TFOOT element, it must
                >> appear before the first TBODY.[/color]
                >
                > Please clarify "must".[/color]

                ....
                [color=blue]
                > Will it fail on some browsers?[/color]

                Who knows, considering ...
                [color=blue]
                > Will it not validate with W3C?[/color]

                It won't. It's not valid HTML.

                From
                <URL:http://www.w3.org/TR/html4/struct/tables.html#ede f-TABLE>
                "<!ELEMENT TABLE - -
                (CAPTION?, (COL*|COLGROUP* ), THEAD?, TFOOT?, TBODY+)>"
                and
                "TFOOT must appear before TBODY within a TABLE definition"

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Michael Winter

                  #9
                  Re: OT: proper HTML (was Re: create a new row in a table)

                  On Wed, 29 Sep 2004 09:02:19 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
                  wrote:
                  [color=blue]
                  > In article <opsez6hwiix13k vk@atlantis>, M.Winter@blueyo nder.co.invalid
                  > enlightened us with...[/color]

                  [snip]
                  [color=blue][color=green]
                  >> If a table contains a TFOOT element, it must appear before the first
                  >> TBODY.[/color]
                  >
                  > Please clarify "must".[/color]

                  From section 11.2.3 - Row groups: the THEAD, TFOOT, and TBODY elements:

                  "This example illustrates the order and structure of table heads,
                  feet, and bodies.

                  <TABLE>
                  <THEAD>
                  <TR> ...header information...
                  </THEAD>
                  <TFOOT>
                  <TR> ...footer information...
                  </TFOOT>
                  <TBODY>
                  <TR> ...first row of block one data...
                  <TR> ...second row of block one data...
                  </TBODY>
                  <TBODY>
                  <TR> ...first row of block two data...
                  <TR> ...second row of block two data...
                  <TR> ...third row of block two data...
                  </TBODY>
                  </TABLE>

                  TFOOT must appear before TBODY within a TABLE definition so that
                  user agents can render the foot before receiving all of the
                  (potentially numerous) rows of data."
                  [color=blue]
                  > I have used tfoot and placed it after the tbody with no problems in my
                  > intranet code. Meaning that the table rendered as desired and expected
                  > in NN7 and IE6.
                  > Will it fail on some browsers?[/color]

                  Probably not. You've no doubt seen as much tag soup as I have, yet
                  browsers still do something meaningful with it. The browser will probably
                  just error-correct the tree to include the TFOOT element before any TBODY
                  elements.
                  [color=blue]
                  > Will it not validate with W3C?[/color]

                  No, it will not. The validator requires the order in the example above.

                  Upon reflection, I think the only significance to the order was what I
                  said in my last post: the footer should come first so the browser can
                  render it before the body. However, I doubt many attempt to do so.
                  Nevertheless, the mark-up is invalid. I don't think such a simple
                  correction should require any kind of justification.

                  Mike

                  --
                  Michael Winter
                  Replace ".invalid" with ".uk" to reply by e-mail.

                  Comment

                  • kaeli

                    #10
                    Re: OT: proper HTML (was Re: create a new row in a table)

                    In article <opse3jv4gcx13k vk@atlantis>, M.Winter@blueyo nder.co.invalid
                    enlightened us with...[color=blue]
                    >
                    > TFOOT must appear before TBODY within a TABLE definition so that
                    > user agents can render the foot before receiving all of the
                    > (potentially numerous) rows of data."
                    >[/color]

                    Ah, okey dokey.
                    [color=blue]
                    > Nevertheless, the mark-up is invalid. I don't think such a simple
                    > correction should require any kind of justification.
                    >[/color]

                    It does to me.
                    I like to know why I'm doing something as illogical as writing a footer at
                    the top of a table before body content. ;)
                    YMMV.


                    --
                    --
                    ~kaeli~
                    A lot of money is tainted - It taint yours and it taint mine.



                    Comment

                    • Grant Wagner

                      #11
                      Re: OT: proper HTML (was Re: create a new row in a table)

                      Michael Winter wrote:
                      [color=blue][color=green]
                      > > I have used tfoot and placed it after the tbody with no problems in my
                      > > intranet code. Meaning that the table rendered as desired and expected
                      > > in NN7 and IE6.
                      > > Will it fail on some browsers?[/color]
                      >
                      > Probably not. You've no doubt seen as much tag soup as I have, yet
                      > browsers still do something meaningful with it. The browser will probably
                      > just error-correct the tree to include the TFOOT element before any TBODY
                      > elements.[/color]

                      The problem is that:

                      <table>
                      <thead>
                      <tr><td>Head</td></tr>
                      </thead>
                      <tfoot>
                      <tr><td>Foot</td></tr>
                      </tfoot>
                      <tbody>
                      <tr><td>Body</td></tr>
                      </tbody>
                      </table>

                      Renders on Netscape 4.78 as:

                      Head
                      Foot
                      Body

                      [color=blue]
                      > Upon reflection, I think the only significance to the order was what I
                      > said in my last post: the footer should come first so the browser can
                      > render it before the body. However, I doubt many attempt to do so.
                      > Nevertheless, the mark-up is invalid. I don't think such a simple
                      > correction should require any kind of justification.[/color]

                      Given that most modern user agents will, as you say, correct the incorrect
                      THEAD, TBODY, TFOOT layout, but Netscape 4.78 (and potentially other less
                      capable user agents on portable devices) may render THEAD, TFOOT, TBODY
                      incorrectly, it seems prudent at this point in HTML authoring to either omit
                      these elements entirely, or place them in what at first blush would appear to
                      be their logical order (or use TBODY for everything and control any CSS
                      issues using classes).

                      For example:

                      tfoot td {font-weight:bold;}

                      might become:

                      tbody.reallyTfo ot td {font-weight:bold;}

                      Now, admittedly Netscape 4.78 won't honor either of these, but having the
                      cells in the tfoot appear bold isn't nearly as important as having the cells
                      in the tfoot appear at the bottom of the table.

                      --
                      Grant Wagner <gwagner@agrico reunited.com>
                      comp.lang.javas cript FAQ - http://jibbering.com/faq

                      Comment

                      Working...