Trying to set text in a td through dom

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

    Trying to set text in a td through dom

    Hello,

    What's up with this?

    I've got a <td id="container" > and want to set the text in this cell like
    ....

    var container = document.getEle mentById('conta iner');
    container.data = "Data in cell";

    but can only seem to do it like this ...

    var label = document.create TextNode("Data in cell");
    container.appen dChild(label);

    which seems to actually leave me with two text nodes.


    Thanks,

    Bill


  • Bill M.

    #2
    Re: Trying to set text in a td through dom

    Ok ... It appears that you need *something* to start with between the <td>
    and the </td>

    So I have ...

    <td id=container>&n bsp;</td>

    then I can do ..

    var container = document.getEle mentById('conta iner');
    container.data = "Data in cell";

    But this is strange because there should exist a text node (CharacterData)
    for every element, even if there is no text; at least as I understand the
    model.


    "Bill M." <wpmccormick@ho tmail.com> wrote in message
    news:6c9c3$3fb1 7835$44a52955$7 763@msgid.megan ewsservers.com. ..[color=blue]
    > Hello,
    >
    > What's up with this?
    >
    > I've got a <td id="container" > and want to set the text in this cell like
    > ...
    >
    > var container = document.getEle mentById('conta iner');
    > container.data = "Data in cell";
    >
    > but can only seem to do it like this ...
    >
    > var label = document.create TextNode("Data in cell");
    > container.appen dChild(label);
    >
    > which seems to actually leave me with two text nodes.
    >
    >
    > Thanks,
    >
    > Bill
    >
    >[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Trying to set text in a td through dom

      "Bill M." <wpmccormick@ho tmail.com> writes:

      [topposting fixed][color=blue]
      > "Bill M." <wpmccormick@ho tmail.com> wrote in message
      > news:6c9c3$3fb1 7835$44a52955$7 763@msgid.megan ewsservers.com. ..[/color]
      [color=blue][color=green]
      >> I've got a <td id="container" > and want to set the text in this cell like
      >> ...
      >>
      >> var container = document.getEle mentById('conta iner');
      >> container.data = "Data in cell";[/color][/color]

      If the td has a text node inside it, you can use
      container.first Child.nodeValue = "Data in cell";
      [color=blue][color=green]
      >> but can only seem to do it like this ...
      >>
      >> var label = document.create TextNode("Data in cell");
      >> container.appen dChild(label);[/color][/color]

      That works too.
      [color=blue][color=green]
      >> which seems to actually leave me with two text nodes.[/color][/color]

      It does. You can remove the existing text node first:
      container.remov eChild(containe r.firstChild);

      [color=blue]
      > Ok ... It appears that you need *something* to start with between the <td>
      > and the </td>
      >
      > So I have ...
      >
      > <td id=container>&n bsp;</td>
      >
      > then I can do ..
      >
      > var container = document.getEle mentById('conta iner');
      > container.data = "Data in cell";[/color]

      Does it work? I didn't think the td element had a "data" property.
      However,
      container.first Child.data = "Data in cell";
      would work (equivalent to .nodeValue).
      [color=blue]
      > But this is strange because there should exist a text node (CharacterData)
      > for every element, even if there is no text; at least as I understand the
      > model.[/color]

      I don't think so. Every CharacterData has a "data" property, but the td
      *Element* is not a CharacterData.
      The inheritance hierarchy is:
      +------+
      |/Node/|
      +------+
      / \
      +-------+ +---------------+
      |Element| |/CharacterData/|
      +-------+ +---------------+
      \
      +----+
      |Text|
      +----+

      The td element is an Element. Its first child node is a Text.

      You try to set the "data" property of something that is not a
      CharacterData. That just creates a new property, but otherwise
      does nothing.

      /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

      • Bill M.

        #4
        Re: Trying to set text in a td through dom

        [snip]
        [color=blue]
        > Does it work? I didn't think the td element had a "data" property.
        > However,
        > container.first Child.data = "Data in cell";
        > would work (equivalent to .nodeValue).[/color]

        Sorry my BAD; I left out the firstChild. However, this still appears not to
        work unless there is some text already there.
        [color=blue]
        >[color=green]
        > > But this is strange because there should exist a text node[/color][/color]
        (CharacterData)[color=blue][color=green]
        > > for every element, even if there is no text; at least as I understand[/color][/color]
        the[color=blue][color=green]
        > > model.[/color]
        >
        > I don't think so. Every CharacterData has a "data" property, but the td
        > *Element* is not a CharacterData.
        > The inheritance hierarchy is:
        > +------+
        > |/Node/|
        > +------+
        > / \
        > +-------+ +---------------+
        > |Element| |/CharacterData/|
        > +-------+ +---------------+
        > \
        > +----+
        > |Text|
        > +----+
        >
        > The td element is an Element. Its first child node is a Text.
        >
        > You try to set the "data" property of something that is not a
        > CharacterData. That just creates a new property, but otherwise
        > does nothing.
        >[/color]

        Ok. I see. I'm getting messed up by peering too deeply into my Mozilla DOM
        Inspector. Every Element appears as a Node with a text child node whether or
        not the text is there.

        Why DOM Inspector is showing text nodes when they're not initialized I'm not
        sure. I think they must be there in some state anyway. The odd thing is how
        this 'sleeping' textnode appears to go unused when you append a text node.
        That is, DOM Inspector will show 2 text nodes (that could be normalized I
        guess) after appending a text node to a <td> element.

        cheers,

        Bill


        Comment

        Working...