Modify table cell...

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

    Modify table cell...

    I have a table with a cell. The cell's ID is created using a unique name
    that
    is held in m_UniqueCellNam e and the cell is created like so...

    document.write( "<TD ID="' + m_UniqueCellNam e + '"></TD>" );

    How can I programmaticall y modify the contents of the cell whose name
    is held within m_UniqueCellNam e? The variable will get passed around
    to other functions, but try as I might,

    myid = document.getEle mentById( m_UniqueCellNam e );

    document.all.my id.innerHTML = "Something! ";

    simply doesn't work. What is it that I'm missing? Any help would be
    greatly appreciated.


  • Ivo

    #2
    Re: Modify table cell...

    "Anon" wrote[color=blue]
    > How can I programmaticall y modify the contents of the cell whose name
    > is held within m_UniqueCellNam e? The variable will get passed around
    > to other functions, but try as I might,
    >
    > myid = document.getEle mentById( m_UniqueCellNam e );
    >
    > document.all.my id.innerHTML = "Something! ";
    >
    > simply doesn't work.[/color]

    You made myid reference to the object itself, not to its name, which is what
    document.all expects. Try:

    myid.innerHTML= "Yes!";
    or
    document.all[ m_UniqueCellNam e ].innerHTML = "Something! ";

    Note the dot following the closing ] and the absence of one preceding the [.
    The first option is the prefered one as document.all is not part of any
    standard (neither is innerHTML btw).

    hth
    --
    Ivo



    Comment

    • RobG

      #3
      Re: Modify table cell...

      Anon wrote:[color=blue]
      > I have a table with a cell. The cell's ID is created using a unique[/color]
      name[color=blue]
      > that
      > is held in m_UniqueCellNam e and the cell is created like so...
      >
      > document.write( "<TD ID="' + m_UniqueCellNam e + '"></TD>" );
      >
      > How can I programmaticall y modify the contents of the cell whose name
      > is held within m_UniqueCellNam e? The variable will get passed around
      > to other functions, but try as I might,
      >
      > myid = document.getEle mentById( m_UniqueCellNam e );
      >
      > document.all.my id.innerHTML = "Something! ";
      >
      > simply doesn't work. What is it that I'm missing? Any help would be
      > greatly appreciated.[/color]

      When you create m_UniqueCellNam e, it is just a string that has
      no special properties.

      When you use document.write, you have created HTML that is
      parsed by the browser and rendered in a browser window. You
      never created a reference to the cell you create, hence you
      don't have one to 'remember'. m_UniqueCellNam e will only refer
      to the original variable which contains a string.

      If you want to create the cell in a manner that allows you to
      reference it later, use DOM methods to build your table. Or,
      if you create m_UniqueCellNam e as global variable (not recommended),
      you can later get a reference to the cell using:


      if (document.getEl ementById) {
      var myid = document.getEle mentById(m_Uniq ueCellName);
      } else if (document.all) {
      var mtid = document.all(m_ UniqueCellName) ;
      }

      myid.innerHTML = 'something!';

      As noted elsewhere, innerHTML is a Microosft invention that is
      not part of any W3C standard, it should be used judiciously.
      If you want to manipulate the contents of a table cell you
      should use DOM methods, however if all that is required is the
      simple replacement of some text in the cell, it is fine in most
      cases.

      --
      Rob

      Comment

      • Anon

        #4
        Re: Modify table cell...


        "Ivo" <no@thank.you > wrote in message
        news:4230cdaa$0 $190$dbd4d001@n ews.wanadoo.nl. ..
        [color=blue]
        > You made myid reference to the object itself, not to its name, which is
        > what
        > document.all expects. Try:
        >
        > myid.innerHTML= "Yes!";
        > or
        > document.all[ m_UniqueCellNam e ].innerHTML = "Something! ";[/color]

        Thanks Ivo, the second method works perfectly, however I'd feel more
        comfortable if I could get the first method working too. ;-)


        Comment

        • Mick White

          #5
          Re: Modify table cell...

          Anon wrote:
          [color=blue]
          > I have a table with a cell. The cell's ID is created using a unique name
          > that
          > is held in m_UniqueCellNam e and the cell is created like so...
          >
          > document.write( "<TD ID="' + m_UniqueCellNam e + '"></TD>" );[/color]


          document.write( "<TD ID='" + m_UniqueCellNam e + "'></TD>" );

          Mick

          Comment

          • Anon

            #6
            Re: Modify table cell...


            "RobG" <rgqld@iinet.ne t.au> wrote in message
            news:1110496191 .673993.4440@l4 1g2000cwc.googl egroups.com...
            [color=blue]
            > As noted elsewhere, innerHTML is a Microosft invention that is
            > not part of any W3C standard, it should be used judiciously.
            > If you want to manipulate the contents of a table cell you
            > should use DOM methods, however if all that is required is the
            > simple replacement of some text in the cell, it is fine in most
            > cases.[/color]

            I used the DOM methods to create a Table, then added a row like so,

            var cell = document.create Element( "TD" );
            cell.className = m_UniqueCellNam e ;
            cell.appendChil d( document.create TextNode( "Some Cell" ) );

            However, the following code only works in IE and Opera, but not NN
            or Firefox (all browsers are the latest version):

            m_UniqueCellNam e.document.writ e( "hello" );

            But there's also the added effect that (in IE and Opera), while the cell
            contents are indeed changed, the entire contents of the page are erased.


            Comment

            • Evertjan.

              #7
              Re: Modify table cell...

              Anon wrote on 12 mrt 2005 in comp.lang.javas cript:
              [color=blue]
              >
              > "RobG" <rgqld@iinet.ne t.au> wrote in message
              > news:1110496191 .673993.4440@l4 1g2000cwc.googl egroups.com...
              >[color=green]
              >> As noted elsewhere, innerHTML is a Microosft invention that is
              >> not part of any W3C standard, it should be used judiciously.
              >> If you want to manipulate the contents of a table cell you
              >> should use DOM methods, however if all that is required is the
              >> simple replacement of some text in the cell, it is fine in most
              >> cases.[/color]
              >
              > I used the DOM methods to create a Table, then added a row like so,
              >
              > var cell = document.create Element( "TD" );
              > cell.className = m_UniqueCellNam e ;[/color]

              m_UniqueCellNam e returns a string? Or is it a function?
              [color=blue]
              > cell.appendChil d( document.create TextNode( "Some Cell" ) );
              >
              > However, the following code only works in IE and Opera, but not NN
              > or Firefox (all browsers are the latest version):[/color]

              You cannot use a class [className] as a name or an id

              You should not use a name as an id, and let the name be used as an
              object variable, when wanting to be somewhat cross browser compatible.

              You should use an id and use getElementById( ) to be somewhat cross
              browser compatible.
              [color=blue]
              > m_UniqueCellNam e.document.writ e( "hello" );[/color]

              m_UniqueCellNam e.document.writ e()

              The cell [that is referenced incorrectly, see above,] is NOT a child of
              the document. Your code is nonsensical. Review in your mind the DOM tree.
              [color=blue]
              >
              > But there's also the added effect that (in IE and Opera), while the
              > cell contents are indeed changed, the entire contents of the page are
              > erased.[/color]

              A logical consequence of using document.write( ) in a finished page.
              Then document.write( ) calls document.open() first.

              Is m_UniqueCellNam e perhaps an alias of window[]?

              --
              Evertjan.
              The Netherlands.
              (Replace all crosses with dots in my emailaddress)

              Comment

              Working...