473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.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
Jul 20 '05 #1
3 8601
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." <wp*********@hotmail.com> wrote in message
news:6c**************************@msgid.meganewsse 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

Jul 20 '05 #2
"Bill M." <wp*********@hotmail.com> writes:

[topposting fixed]
"Bill M." <wp*********@hotmail.com> wrote in message
news:6c**************************@msgid.meganewsse 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
[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
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kenneth P | last post by:
Hi, I have a very good book on asp.net Sams ASP.NET Tips, Tutorials and Code ISBN 0-672-32143-2 and is trying to teach myself from examples from this book Chapter 16. Anyway I come quite...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
3
by: The One | last post by:
Have created a form to pop up with 2 option I then wish to write the text that is in the option button chose back to the original form using the code below but it gives me an exception error so...
14
by: deercreek | last post by:
Could use some help. I don't know if I can even do whatI'm trying to do. I have a text box in a report under the details section. It will return a value for each line on a sepcific record set....
4
by: 28tommy | last post by:
Hi, I'm trying to find scripts in html source of a page retrieved from the web. I'm trying to use the following rule: match = re.compile('<script + src=+>') I'm testing it on a page that...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
11
by: =?Utf-8?B?TWlrZSBDb2xsaW5z?= | last post by:
I am trying to get the text of an item in a GridView, but am doing something wrong. Can someone help me with the correct C# statement I need? Below is my GridView and my attempt to get the control....
1
by: rsteph | last post by:
I've got some product information pages, with images and text, all setup within a table. I'm trying to add a small image in the upper right hand corner of the content div (where all the important...
5
by: BlueDream | last post by:
What I'm trying to do is simply scan in a file then whenever I get a space, turn it into 2 spaces. Whenever I get a carriage return, turn it into 2 spaces.. An example of what I'm trying to...
6
by: Doogie | last post by:
Hi I have an img control I am trying to hide upon certain types of commands in my code behind. When to hide it is directly tied to a asp:dropdownlist control. So depending on what the user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.