Trying to set text in a td through dom

Discussion in 'Javascript' started by Bill M., Nov 12, 2003.

  1. Bill M.

    Bill M. Guest

    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.getElementById('container');
    container.data = "Data in cell";

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

    var label = document.createTextNode("Data in cell");
    container.appendChild(label);

    which seems to actually leave me with two text nodes.


    Thanks,

    Bill
    Bill M., Nov 12, 2003
    #1
    1. Advertising

  2. Bill M.

    Bill M. Guest

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

    So I have ...

    <td id=container>&nbsp;</td>

    then I can do ..

    var container = document.getElementById('container');
    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." <> wrote in message
    news:6c9c3$3fb17835$44a52955$...
    > 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.getElementById('container');
    > container.data = "Data in cell";
    >
    > but can only seem to do it like this ...
    >
    > var label = document.createTextNode("Data in cell");
    > container.appendChild(label);
    >
    > which seems to actually leave me with two text nodes.
    >
    >
    > Thanks,
    >
    > Bill
    >
    >
    Bill M., Nov 12, 2003
    #2
    1. Advertising

  3. "Bill M." <> writes:

    [topposting fixed]
    > "Bill M." <> wrote in message
    > news:6c9c3$3fb17835$44a52955$...


    >> I've got a <td id="container"> and want to set the text in this cell like
    >> ...
    >>
    >> var container = document.getElementById('container');
    >> container.data = "Data in cell";


    If the td has a text node inside it, you can use
    container.firstChild.nodeValue = "Data in cell";

    >> but can only seem to do it like this ...
    >>
    >> var label = document.createTextNode("Data in cell");
    >> container.appendChild(label);


    That works too.

    >> which seems to actually leave me with two text nodes.


    It does. You can remove the existing text node first:
    container.removeChild(container.firstChild);


    > Ok ... It appears that you need *something* to start with between the <td>
    > and the </td>
    >
    > So I have ...
    >
    > <td id=container>&nbsp;</td>
    >
    > then I can do ..
    >
    > var container = document.getElementById('container');
    > container.data = "Data in cell";


    Does it work? I didn't think the td element had a "data" property.
    However,
    container.firstChild.data = "Data in cell";
    would work (equivalent to .nodeValue).

    > 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.


    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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Nov 12, 2003
    #3
  4. Bill M.

    Bill M. Guest

    [snip]

    > Does it work? I didn't think the td element had a "data" property.
    > However,
    > container.firstChild.data = "Data in cell";
    > would work (equivalent to .nodeValue).


    Sorry my BAD; I left out the firstChild. However, this still appears not to
    work unless there is some text already there.

    >
    > > 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.

    >
    > 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.
    >


    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
    Bill M., Nov 12, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thorsten Meininger
    Replies:
    0
    Views:
    439
    Thorsten Meininger
    Jul 28, 2004
  2. Thorsten Meininger
    Replies:
    0
    Views:
    508
    Thorsten Meininger
    Jul 28, 2004
  3. mike
    Replies:
    1
    Views:
    1,139
    Martin Honnen
    Nov 20, 2004
  4. Replies:
    0
    Views:
    548
  5. Tom Fitzgibbon

    loop through text object values in DOM?

    Tom Fitzgibbon, Dec 4, 2003, in forum: Javascript
    Replies:
    2
    Views:
    110
    Tom Fitzgibbon
    Dec 4, 2003
Loading...

Share This Page