DOM problems

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

    DOM problems

    I am still having problems with the dom. blah ...

    I have a table like:

    <tbody id="list">
    <tr>
    <td>a</td> <td>b</td> <td>c</td> <td>d</td>

    </tr>
    </tbody>

    To get the values "a", "b", "c", and "d" I have this code:

    // get a list of all the tbody elements (there will only be one)

    var my_tbody = document.getEle mentsByTagName( "tbody").item(0 );

    //the first tr element under tbody
    var my_row = my_tbody.getEle mentsByTagName( "tr").item( 0);

    //the first td element under tr
    var my_cell = my_row.getEleme ntsByTagName("t d").item(0);

    //look at each td cell value
    for( i=0; i<my_cell.child Nodes.length; i++ )
    {
    mycelnode = my_cell.childNo des.item(i);
    mytext = mycelnode.nodeV alue;
    alert(mytext);
    }

    To update the values to "x" I have this code:

    //look at each value
    for( i=0; i<my_cell.child Nodes.length; i++ )
    {
    mycelnode = my_cell.childNo des.item(i);
    mytext = mycelnode.nodeV alue = "x";
    }

    This is not working. Any help is appreciated.

    Mike

  • Lee

    #2
    Re: DOM problems

    Michael Hill said:[color=blue]
    >
    >I am still having problems with the dom. blah ...[/color]

    [color=blue]
    > //the first td element under tr
    > var my_cell = my_row.getEleme ntsByTagName("t d").item(0);
    >
    > //look at each td cell value
    > for( i=0; i<my_cell.child Nodes.length; i++ )
    > {
    > mycelnode = my_cell.childNo des.item(i);
    > mytext = mycelnode.nodeV alue;
    > alert(mytext);
    > }[/color]

    That loop does not look at each td cell value.
    It looks at each childNode of my_cell, which is the first td element.
    You need to loop through the childNodes of the tr tag, and alert
    the first childNode of each of those td nodes.


    [color=blue]
    >This is not working. Any help is appreciated.[/color]

    "This is not working" is not useful information.
    You should tell us what error messages or misbehavior you see.

    Comment

    • Michael Hill

      #3
      Re: DOM problems

      >[color=blue]
      > That loop does not look at each td cell value.
      > It looks at each childNode of my_cell, which is the first td element.
      > You need to loop through the childNodes of the tr tag, and alert
      > the first childNode of each of those td nodes.
      >[/color]

      OK, this is a little better but I am still not getting a look at the td
      elements

      // get a list of all the tbody elements (there will only be one)
      var my_tbody=docume nt.getElementsB yTagName("tbody ").item(0);

      //element itself is the first item of the list
      var my_row = my_tbody.getEle mentsByTagName( "tr");

      alert("number of tr elements: " + my_row.length);

      for( i=0; i<my_row.length ; i++ )
      {
      var my_cell = my_row.getEleme ntsByTagName("t d").item(i);
      alert("number of td elements: " + my_cell.length) ;
      for( j=0; j<my_cell.lengt h; j++ )
      {
      myrownode = my_cell.item(j) .nodeName;
      alert(myrownode );
      }
      }


      Comment

      • Eric Bohlman

        #4
        Re: DOM problems

        "Michael Hill" <hillmw@charter .net> wrote in
        news:vso9upb2ds 8m3b@corp.super news.com:
        [color=blue]
        > OK, this is a little better but I am still not getting a look at the td
        > elements
        >
        > // get a list of all the tbody elements (there will only be one)
        > var my_tbody=docume nt.getElementsB yTagName("tbody ").item(0);
        >
        > //element itself is the first item of the list
        > var my_row = my_tbody.getEle mentsByTagName( "tr");
        >
        > alert("number of tr elements: " + my_row.length);
        >
        > for( i=0; i<my_row.length ; i++ )
        > {[/color]

        Here you're looping over each row. I thought you were only interested in
        the first row?
        [color=blue]
        > var my_cell = my_row.getEleme ntsByTagName("t d").item(i);[/color]

        Here you get the i'th TD from the i'th row, that is, column 1 from row 1,
        column 2 from row 2, and so on. IOW, you're going down the diagonal of the
        table rather than across the rows.
        [color=blue]
        > alert("number of td elements: " + my_cell.length) ;[/color]

        It's always showing you "1", right?
        [color=blue]
        > for( j=0; j<my_cell.lengt h; j++ )
        > {[/color]

        Now you're looping over a single element.
        [color=blue]
        > myrownode = my_cell.item(j) .nodeName;
        > alert(myrownode );
        > }
        > }[/color]

        Comment

        • Michael Hill

          #5
          Re: DOM problems

          [color=blue]
          > Here you're looping over each row. I thought you were only interested in
          > the first row?
          >[color=green]
          > > var my_cell = my_row.getEleme ntsByTagName("t d").item(i);[/color]
          >[/color]

          I have a number of problems:

          // get a list of all the tbody elements (there will only be one)
          var my_tbody=docume nt.getElementsB yTagName("tbody ").item(0);

          //element itself is the first item of the list
          var my_row = my_tbody.getEle mentsByTagName( "tr");

          This statement is retrieving "all" the tr elements in the document. I
          thought I was getting only the 'tr' elements under the node my_tbody.

          I'd like to start with the 'tbody' tag and walk down through to the cell
          contents.

          Mike


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: DOM problems

            Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
            [color=blue]
            > I am still having problems with the dom. blah ...
            >
            > I have a table like:
            >
            > <tbody id="list">
            > <tr>
            > <td>a</td> <td>b</td> <td>c</td> <td>d</td>
            >
            > </tr>
            > </tbody>
            >
            > To get the values "a", "b", "c", and "d" I have this code:[/color]

            ....
            Accessing a table's cells is easier using the rows and cells collections.
            [color=blue]
            > //look at each td cell value
            > for( i=0; i<my_cell.child Nodes.length; i++ )[/color]

            You are running through the child nodes of the first cell, so you
            should only encounter "a".

            Try giving the table an id, and then:
            ---
            var cells = document.getEle mentById("table Id").rows[0].cells;
            for (var i=0; i<cells.length ; i++) {
            var cell = cells.item(i);
            for (var chld = cell.firstChild ;chld;chld=chld .nextSibiling) {
            if (chld.nodeType == 3) { //text node
            // something with nodeValue, e.g.,
            alert(chld.node Value);
            }
            }
            }
            ---
            --
            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 Hill

              #7
              Re: DOM problems

              Lasse,

              As always you have great answers. I modified the code you provided to look at
              more than one row as follows:

              var rows = document.getEle mentById("table Id").rows;
              for (var j=0; j<rows.length; j++)
              {
              var cells = document.getEle mentById("table Id").rows[j].cells;
              ...............

              Question, though, see below .......

              Lasse Reichstein Nielsen wrote:
              [color=blue]
              > ......
              >
              > Try giving the table an id, and then:
              > ---
              > var cells = document.getEle mentById("table Id").rows[0].cells;
              > for (var i=0; i<cells.length ; i++) {
              > var cell = cells.item(i);
              > for (var chld = cell.firstChild ;chld;chld=chld .nextSibiling) {[/color]

              Lasse, what is going on here ?
              [color=blue]
              >
              > if (chld.nodeType == 3) { //text node
              > // something with nodeValue, e.g.,
              > alert(chld.node Value);
              > }
              > }
              > }
              > ---
              > --
              > 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.'[/color]

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: DOM problems

                Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
                [color=blue]
                > var rows = document.getEle mentById("table Id").rows;
                > for (var j=0; j<rows.length; j++)
                > {
                > var cells = document.getEle mentById("table Id").rows[j].cells;[/color]

                Or just:
                var cells = rows[j].cells;
                You already found the rows above.
                [color=blue][color=green]
                >> for (var chld = cell.firstChild ;chld;chld=chld .nextSibiling) {[/color]
                >
                > Lasse, what is going on here ?[/color]

                This loop is running through the child nodes of "cell". Instead
                of doing it by number:
                for (var i=0;i<cell.chil dNodes.length;i ++) {
                var chld = cell.childNodes[i];
                ...
                }
                It starts from the first child (cell.firstChil d) and follows the
                nextSibling references through the child nodes. It stops when
                "chld" becomes a false value, which would be after the last node.
                The last node has .nextSibling == null, and null converts to false.

                /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 Hill

                  #9
                  Re: DOM problems


                  "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                  news:ekvn6k8t.f sf@hotpop.com.. .[color=blue]
                  > Michael Hill <hillmw@ram.lmt as.lmco.com> writes:
                  >[color=green]
                  > > var rows = document.getEle mentById("table Id").rows;
                  > > for (var j=0; j<rows.length; j++)
                  > > {
                  > > var cells = document.getEle mentById("table Id").rows[j].cells;[/color]
                  >[/color]

                  I would think then that I could remove the rows using:

                  var rows = document.getEle mentById("list" ).rows;
                  for ( var j=0; j<rows.length; j++ )
                  {
                  alert(rows[j].nodeName); // produces TR
                  var mytr = rows[j];
                  rows.removeChil d(mytr);
                  }


                  Comment

                  • Lasse Reichstein Nielsen

                    #10
                    Re: DOM problems

                    "Michael Hill" <hillmw@charter .net> writes:
                    [color=blue]
                    > I would think then that I could remove the rows using:[/color]
                    [color=blue]
                    > var mytr = rows[j];
                    > rows.removeChil d(mytr);[/color]

                    "rows" is a collection, not a node. What you want is
                    mytr.parentNode .removeChild(my tr);

                    /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 Hill

                      #11
                      Re: DOM problems


                      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
                      news:oeuqgyjs.f sf@hotpop.com.. .[color=blue]
                      > "Michael Hill" <hillmw@charter .net> writes:
                      >[color=green]
                      > > I would think then that I could remove the rows using:[/color]
                      >[/color]
                      And I would think I could add rows ( of course this would only add 1 cell )
                      using:

                      var rows = document.getEle mentById("list" ).rows;
                      var mytr = rows;
                      // Creates separated nodes.
                      mycurrentRow=my tr.createElemen t("TR");
                      mycurrentCell=m ytr.createEleme nt("TD");
                      mycurrentText=m ytr.createTextN ode("a generic text");

                      // Appends each node following the structure.
                      mycurrentCell.a ppendChild(mycu rrentText);
                      mycurrentRow.ap pendChild(mycur rentCell);
                      mytr.appendChil d(mycurrentRow) ;


                      Comment

                      Working...