writing to a give table element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mxa@yahoo.com

    #1

    writing to a give table element

    Hi,
    given table :
    <table id='t1'>
    <tr><td> data1</td><td>data2</td></tr>
    </table>

    i would like to write a function to write and change data2
    ( location is fixed ).

    function changeme () {

    document.write( 'new data2');

    }

    how can I focus on 'data2' before calling the above function?
    thanks
    Michael

  • RobB

    #2
    Re: writing to a give table element

    mxa@yahoo.com wrote:[color=blue]
    > Hi,
    > given table :
    > <table id='t1'>
    > <tr><td> data1</td><td>data2</td></tr>
    > </table>
    >
    > i would like to write a function to write and change data2
    > ( location is fixed ).
    >
    > function changeme () {
    >
    > document.write( 'new data2');
    >
    > }
    >
    > how can I focus on 'data2' before calling the above function?
    > thanks
    > Michael[/color]

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    //<![CDATA[

    function changeme()
    {
    var tbl = document.getEle mentById('t1'); //table
    var firstrow = tbl.rows.item(0 ); //row 1 (DOM: 0)
    var cell2 = firstrow.cells. item(1); //2nd TD
    while (cell2.hasChild Nodes()) //anything in there?
    cell2.removeChi ld(cell2.lastCh ild); //adios
    cell2.appendChi ld(document.cre ateTextNode('ne w data2')); //new text
    node, add
    }

    onload = function()
    {
    setTimeout(chan geme, 3000);
    }

    //]]>
    </script>
    </head>
    <body>
    <table id="t1" cellspacing="10 ">
    <tbody style="font:32p x tahoma;">
    <tr>
    <td>data1</td>
    <td>data2</td>
    </tr>
    </tbody>
    </table>
    </body>
    </html>

    http://www.sitepoint.com/article/rough-guide-dom

    Comment

    • RobB

      #3
      Re: writing to a give table element

      Just a note: could also go -

      var tbody =
      document.getEle mentById('t1'). getElementsByTa gName('tbody'). item(0);
      var firstrow = tbody.rows.item (0);
      Another ref:



      Comment

      • Fred Oz

        #4
        Re: writing to a give table element

        RobB wrote:
        [...]

        Now there's an easy way and a hard way...

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        <title>Change Data2</title>

        <script type="text/javascript">
        function changeme(ele,st r) {
        var tbl = document.getEle mentById(ele);
        t1.innerHTML = str;
        }
        </script>

        </head>
        <body>
        <table cellspacing="10 ">
        <tbody style="font:32p x tahoma;">
        <tr>
        <td>data1</td>
        <td id="t1">data2</td>
        </tr>
        </tbody>
        </table>

        <div onclick="change me('t1','Here is the new text');" style="
        font-family: tahoma;
        border: 1px solid blue;
        background-color: #ddddee;
        width: 20em;">Click here to to change the text</div>
        </body>
        </html>

        I reckon that's the easy way! :-)

        Of course, getElementsById is not compatible with older browsers and
        innerHTML, whilst well supported, is not part of the official DOM. So
        if this is for real web use, let us know and we'll post something a bit
        more robust (but it'll take a few more lines of code...)



        --
        Fred

        Comment

        • Fred Oz

          #5
          Re: writing to a give table element

          Fred Oz wrote:[color=blue]
          > RobB wrote:[/color]
          [...][color=blue]
          > <script type="text/javascript">
          > function changeme(ele,st r) {
          > var tbl = document.getEle mentById(ele);
          > t1.innerHTML = str;
          > }
          > </script>[/color]

          Believe it or not, the above rubbish worked in Safari. My apologies,
          the correct code is below.

          <script type="text/javascript">
          function changeme(ele,st r) {
          var d = document.getEle mentById(ele);
          d.innerHTML = str;
          }
          </script>



          --
          Fred

          Comment

          • RobB

            #6
            Re: writing to a give table element

            Fred Oz wrote:[color=blue]
            > Fred Oz wrote:[color=green]
            > > RobB wrote:[/color]
            > [...][color=green]
            > > <script type="text/javascript">
            > > function changeme(ele,st r) {
            > > var tbl = document.getEle mentById(ele);
            > > t1.innerHTML = str;
            > > }
            > > </script>[/color]
            >
            > Believe it or not, the above rubbish worked in Safari. My[/color]
            apologies,[color=blue]
            > the correct code is below.
            >
            > <script type="text/javascript">
            > function changeme(ele,st r) {
            > var d = document.getEle mentById(ele);
            > d.innerHTML = str;
            > }
            > </script>
            >
            >
            >
            > --
            > Fred[/color]

            Fred:
            some leisure reading.



            Comment

            Working...