Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Trying to set text in a td through dom

Reply
Thread Tools

Trying to set text in a td through dom

 
 
Bill M.
Guest
Posts: n/a
 
      11-12-2003
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


 
Reply With Quote
 
 
 
 
Bill M.
Guest
Posts: n/a
 
      11-12-2003
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$ rvers.com...
> 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
>
>



 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-12-2003
"Bill M." <> writes:

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


>> 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.'
 
Reply With Quote
 
Bill M.
Guest
Posts: n/a
 
      11-12-2003
[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert a XML DOM Object to a HTML DOM Object manjunath.d@gmail.com XML 0 09-20-2005 08:16 AM
What is the difference between DOM Level 1 and DOM Level 2. mike XML 1 11-20-2004 03:19 PM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger XML 0 07-28-2004 08:51 AM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger Java 0 07-28-2004 08:51 AM
loop through text object values in DOM? Tom Fitzgibbon Javascript 2 12-04-2003 09:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57