how to write to a table cell??

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

    how to write to a table cell??

    Hi all,

    Just wondering how to write (using document.write) to a table cell. I
    have table with 3 rows and 3 colums. I want to write from within the
    Javascript to say third column of a first row. How do I do it?

    I think one way is to write the entire table out in
    document.write( "<table>... ) but I'd like to know if it is possible to
    access a particular cell in a table.

    If you know of any good online tutorial, could u send me a link?

    Thanks!
    Ben
  • mps@webmind.nl

    #2
    Re: how to write to a table cell??

    I once used the reference:

    tableID.rows[x].cells[y].innerHTML

    Good luck.
    Marco


    "Ben" <crescent_au@ya hoo.com> schreef in bericht
    news:d99e1341.0 407190200.791be 35d@posting.goo gle.com...[color=blue]
    > Hi all,
    >
    > Just wondering how to write (using document.write) to a table cell. I
    > have table with 3 rows and 3 colums. I want to write from within the
    > Javascript to say third column of a first row. How do I do it?
    >
    > I think one way is to write the entire table out in
    > document.write( "<table>... ) but I'd like to know if it is possible to
    > access a particular cell in a table.
    >
    > If you know of any good online tutorial, could u send me a link?
    >
    > Thanks!
    > Ben[/color]


    Comment

    • kaeli

      #3
      Re: how to write to a table cell??

      In article <d99e1341.04071 90200.791be35d@ posting.google. com>,
      crescent_au@yah oo.com enlightened us with...[color=blue]
      > Hi all,
      >
      > Just wondering how to write (using document.write)[/color]

      Why? That's a bad way of doing it that should only be used if you have
      to support very old browsers like NN4.

      If you only have to support newer browsers (IE5+/NN6+/Mozilla/Opera),
      you should be using DOM methods to change the content of table cells.

      I can post code to change the content of table cells using DOM methods
      if you'd like.

      You can't change one cell only using document.write AFAIR. You'd have to
      put the table in a layer and change the whole thing. I haven't had to do
      that in quite some time, so I could be wrong, but IIRC, that's what I
      had to do.

      --
      --
      ~kaeli~
      You can't have everything. Where would you put it?



      Comment

      • Java  script  Dude

        #4
        Re: how to write to a table cell??

        > I think one way is to write the entire table out in[color=blue]
        > document.write( "<table>... ) but I'd like to know if it is possible to
        > access a particular cell in a table.[/color]

        Anything in the body (and even most tags outside) can be written by JS
        using document.write( ). Just add <script>documen t.write("Any html you
        so desire")</script> in your html wherever you wish to write it.

        Suggestion: Add to all your documents or master/general/core js file
        (if you use one)- function dw(s){document. write(s)}. This will save
        you alot of characters with multiple writes saving 11 characters be dw
        instance.

        JsD

        Comment

        • Ben

          #5
          Re: how to write to a table cell??

          kaeli <tiny_one@NOSPA M.comcast.net> wrote in message news:<MPG.1b658 b45e959c868989f ad@nntp.lucent. com>...[color=blue]
          > In article <d99e1341.04071 90200.791be35d@ posting.google. com>,
          > crescent_au@yah oo.com enlightened us with...[color=green]
          > > Hi all,
          > >
          > > Just wondering how to write (using document.write)[/color]
          >
          > Why? That's a bad way of doing it that should only be used if you have
          > to support very old browsers like NN4.
          >
          > If you only have to support newer browsers (IE5+/NN6+/Mozilla/Opera),
          > you should be using DOM methods to change the content of table cells.
          >
          > I can post code to change the content of table cells using DOM methods
          > if you'd like.
          >[/color]
          Can you show me an example??

          Thanx
          Ben

          Comment

          • kaeli

            #6
            Re: how to write to a table cell??

            In article <d99e1341.04071 91706.2c0b5001@ posting.google. com>,
            crescent_au@yah oo.com enlightened us with...[color=blue][color=green]
            > >
            > > I can post code to change the content of table cells using DOM methods
            > > if you'd like.
            > >[/color]
            > Can you show me an example??
            >[/color]

            (jeez but this was harder to figure out cross-browser than just
            dynamically creating a whole table...)


            Okay, tested in NN7.2, Mozilla 1.2, Opera 7.23, and IE6.0.28
            successfully. Make no assumptions about support in other browsers
            without testing them.

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
            <html>
            <head>
            <title>Changi ng table content dynamically with DOM methods</title>
            <script type="text/javascript">
            function changeContent(f rm)
            {
            if (! document.getEle mentById)
            {
            alert("Sorry, your browser doesn't support this.");
            return;
            }

            /* check to make sure we have everything we need. */
            var R = frm.elements["row"].value;
            var C = frm.elements["col"].value;
            var newVal = frm.elements["newVal"].value;
            if (!R || isNaN(R))
            {
            alert("Row must be a number.");
            return;
            }
            if (!C || isNaN(C))
            {
            alert("Column must be a number.");
            return;
            }
            if (! newVal || newVal == "")
            {
            alert("You forgot to give a new value.");
            return;
            }

            /* row and column indexes are 0 based, so subtract one from the
            logical values of 1-4 to make it 0-3 */
            R--;
            C--;

            /* for this example, we are using just plain text in the table cells
            - this gets a little more complicated if other things are in the table
            cells or if the text length is long enough to span multiple nodes */
            var row = document.getEle mentById("tb"). getElementsByTa gName("tr")
            [R];
            var cell = row.getElements ByTagName("td")[C];
            var newCell = document.create Element("td");
            row.replaceChil d(newCell, cell);
            newCell.appendC hild(document.c reateTextNode(n ewVal));
            return;
            }
            </script>
            </head>

            <body>
            <p>A 4 by 4 table...</p>
            <table id="t1" border="1" cellpadding="5" cellspacing="0" >
            <tbody id="tb">
            <tr>
            <td><p>R1 C1</p></td>
            <td><p>R1 C2</p></td>
            <td><p>R1 C3</p></td>
            <td><p>R1 C4</p></td>
            </tr>
            <tr>
            <td><p>R2 C1</p></td>
            <td><p>R2 C2</p></td>
            <td><p>R2 C3</p></td>
            <td><p>R2 C4</p></td>
            </tr>
            <tr>
            <td><p>R3 C1</p></td>
            <td><p>R3 C2</p></td>
            <td><p>R3 C3</p></td>
            <td><p>R3 C4</p></td>
            </tr>
            <tr>
            <td><p>R4 C1</p></td>
            <td><p>R4 C2</p></td>
            <td><p>R4 C3</p></td>
            <td><p>R4 C4</p></td>
            </tr>
            </tbody>
            </table>
            <form id="f1" name="f1">
            <p>Replace which cell?</p>
            <p>
            Row (1-4): <input type="text" name="row" size="3"><br>
            Column (1-4): <input type="text" name="col" size="3">
            </p>
            <p>New value: <input type="text" name="newVal"></p>
            <input type="button" name="b1" value="Try It" onClick="change Content
            (this.form)">
            </form>
            </body>
            </html>

            --
            --
            ~kaeli~
            It was recently discovered that research causes cancer in
            rats.



            Comment

            • Dr John Stockton

              #7
              Re: how to write to a table cell??

              JRS: In article <MPG.1b66fa6f57 11e63b989fbb@nn tp.lucent.com>, dated
              Tue, 20 Jul 2004 10:47:01, seen in news:comp.lang. javascript, kaeli
              <tiny_one@NOSPA M.comcast.net> posted :
              [color=blue]
              > /* check to make sure we have everything we need. */
              > var R = frm.elements["row"].value;
              > var C = frm.elements["col"].value;
              > var newVal = frm.elements["newVal"].value;
              > if (!R || isNaN(R))
              > {
              > alert("Row must be a number.");
              > return;
              > }[/color]

              In practice, Row must be a positive integer; and one can set an upper
              limit on the number of digits it needs.

              So if (!/^\d{1,3}$/.test(R)) // E&OE

              seems a better test.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: how to write to a table cell??

                Java script Dude wrote:

                Who wrote this? <http://netmeister.org/news/learn2quote.htm l>
                vvvvvvvvvvvvvvv[color=blue][color=green]
                >> I think one way is to write the entire table out in
                >> document.write( "<table>... ) but I'd like to know if it is possible to
                >> access a particular cell in a table.[/color]
                >
                > Anything in the body (and even most tags outside) can be written by JS
                > using document.write( ). Just add <script>documen t.write("Any html you
                > so desire")</script> in your html wherever you wish to write it.[/color]

                It should be at least

                <script type="text/javascript">
                document.write( "Any html you so desire");
                </script>
                [color=blue]
                > Suggestion: Add to all your documents or master/general/core js file
                > (if you use one)- function dw(s){document. write(s)}. This will save
                > you alot of characters with multiple writes saving 11 characters be
                > dw instance.[/color]

                var dw = document.write;

                would be better. But since document.write( ) is a host method it should
                be something like

                var dw;
                if (this.document && document.write)
                {
                dw = document.write;
                }
                else
                {
                dw = function() {}
                }

                (in the global execution context).


                PointedEars

                Comment

                • Grant Wagner

                  #9
                  Re: how to write to a table cell??

                  Thomas 'PointedEars' Lahn wrote:
                  [color=blue]
                  > Java script Dude wrote:
                  >
                  > Who wrote this? <http://netmeister.org/news/learn2quote.htm l>
                  > vvvvvvvvvvvvvvv[color=green][color=darkred]
                  > >> I think one way is to write the entire table out in
                  > >> document.write( "<table>... ) but I'd like to know if it is possible to
                  > >> access a particular cell in a table.[/color]
                  > >
                  > > Anything in the body (and even most tags outside) can be written by JS
                  > > using document.write( ). Just add <script>documen t.write("Any html you
                  > > so desire")</script> in your html wherever you wish to write it.[/color]
                  >
                  > It should be at least
                  >
                  > <script type="text/javascript">
                  > document.write( "Any html you so desire");
                  > </script>
                  >[color=green]
                  > > Suggestion: Add to all your documents or master/general/core js file
                  > > (if you use one)- function dw(s){document. write(s)}. This will save
                  > > you alot of characters with multiple writes saving 11 characters be
                  > > dw instance.[/color]
                  >
                  > var dw = document.write;
                  >
                  > would be better. But since document.write( ) is a host method it should
                  > be something like
                  >
                  > var dw;
                  > if (this.document && document.write)
                  > {
                  > dw = document.write;
                  > }
                  > else
                  > {
                  > dw = function() {}
                  > }
                  >
                  > (in the global execution context).[/color]

                  I thought I remembered some issues with assigning document.write to an
                  instance variable and attempting to use it to output text, so I did a small
                  test:

                  <script type="text/javascript">
                  var dw;
                  if (this.document && document.write) {
                  dw = document.write;
                  } else {
                  dw = function() {}
                  }
                  dw("This is a test");
                  </script>

                  IE6SP1: works as expected

                  Mozilla 1.7.1: Error: uncaught exception: [Exception... "Illegal operation
                  on WrappedNative prototype object" nsresult: "0x8057000c
                  (NS_ERROR_XPC_B AD_OP_ON_WN_PRO TO)" location: "JS frame ::
                  file:///C:/DOCUME~1/grantw/LOCALS~1/Temp/hs~new.htm :: <TOP_LEVEL> :: line
                  16" data: no]

                  Netscape 4.78: JavaScript Error: Document.protot ype.write called on
                  incompatible Window

                  Firefox 0.9.2: Error: uncaught exception: [Exception... "Illegal operation
                  on WrappedNative prototype object" nsresult: "0x8057000c
                  (NS_ERROR_XPC_B AD_OP_ON_WN_PRO TO)" location: "JS frame ::
                  file:///C:/DOCUME~1/grantw/LOCALS~1/Temp/hs~new.htm :: <TOP_LEVEL> :: line
                  20" data: no]

                  Opera 6.05 & 7.53: produce no output, generate no JavaScript error

                  Given the lack of support for the approach shown above in some of the most
                  modern browsers, I would say it's probably not recommened.

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

                  Comment

                  Working...