473,787 Members | 2,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to dynamically change text in a table's TD cell?

Can someone tell me how to do this if it is possible?

I have a table based web site, and I would like to dynamically change the
text that is shown in a particular cell of a table. I give the cell the
unique ID label1 so I can get a hold of it, and I can get a javascript to
display the data in an alert box (this is just to see if I can access the
text) with:

mycel = document.getEle mentById('label 1');
myceltext = mycel.childNode s.item(0);
currenttext = myceltext.data;
alert(currentte xt);

But how do I exchange the text in this cell for something else?

Thanks for any help!

steve
Jul 23 '05 #1
5 45952
coolsti wrote:
Can someone tell me how to do this if it is possible?

I have a table based web site, and I would like to dynamically change the
text that is shown in a particular cell of a table. I give the cell the
unique ID label1 so I can get a hold of it, and I can get a javascript to
display the data in an alert box (this is just to see if I can access the
text) with:

mycel = document.getEle mentById('label 1');
myceltext = mycel.childNode s.item(0);
currenttext = myceltext.data;
alert(currentte xt);

But how do I exchange the text in this cell for something else?

Thanks for any help!

steve


mycel = document.getEle mentById('label 1');
while( mycel.childNode s.length )
mycel.removeChi ld( mycel.childNode s[c] );

mycel.appendChi ld(
document.create TextNode( 'new text!' )
);
Or, alternately (I don't recommend this):
document.getEle mentById('label 1').innerHTML =
'new text!';
Missing compatibility checks for brevity's sake, but works in the most
common modern browsers.

Jul 23 '05 #2
On Wed, 01 Jun 2005 01:18:50 -0700, Random wrote:
steve


mycel = document.getEle mentById('label 1');
while( mycel.childNode s.length )
mycel.removeChi ld( mycel.childNode s[c] );

mycel.appendChi ld(
document.create TextNode( 'new text!' )
);
Or, alternately (I don't recommend this):
document.getEle mentById('label 1').innerHTML =
'new text!';
Missing compatibility checks for brevity's sake, but works in the most
common modern browsers.


Hi and thanks!

Your first suggestion is what I in the meantime came up with, and it
works. I thought that it would be possible to directly exchange the text
(perhaps your second alternative is the way to do this) but it works fine
by removing the node and then creating a new one.

I'm using this function:

function changeText(id,s tr) {
// id is the ID of the td cell, str is new text
var mycel = document.getEle mentById(id);
var myceltext = mycel.childNode s.item(0);
mycel.removeChi ld(myceltext);
var newtxt = document.create TextNode(str);
mycel.appendChi ld(newtxt);
}

The main difference here is that I am directly getting the text node and
then removing it only, while your suggestion is removing all nodes in a
loop. But this is probably the same thing in this case? Mine works, anyway.

Thanks again!

steve
Jul 23 '05 #3
coolsti wrote:
On Wed, 01 Jun 2005 01:18:50 -0700, Random wrote:
steve


mycel = document.getEle mentById('label 1');
while( mycel.childNode s.length )
mycel.removeChi ld( mycel.childNode s[c] );

mycel.appendChi ld(
document.create TextNode( 'new text!' )
);
Or, alternately (I don't recommend this):
document.getEle mentById('label 1').innerHTML =
'new text!';
Missing compatibility checks for brevity's sake, but works in the most
common modern browsers.


Hi and thanks!

Your first suggestion is what I in the meantime came up with, and it
works. I thought that it would be possible to directly exchange the text
(perhaps your second alternative is the way to do this) but it works fine
by removing the node and then creating a new one.

I'm using this function:

function changeText(id,s tr) {
// id is the ID of the td cell, str is new text
var mycel = document.getEle mentById(id);
var myceltext = mycel.childNode s.item(0);
mycel.removeChi ld(myceltext);
var newtxt = document.create TextNode(str);
mycel.appendChi ld(newtxt);
}

The main difference here is that I am directly getting the text node and
then removing it only, while your suggestion is removing all nodes in a
loop. But this is probably the same thing in this case? Mine works, anyway.

Thanks again!

steve

The reason for the loop is the differing interpretation of whitespace
between Internet Explorer and Mozilla. Internet Explorer does not
(usually) create a TextNode from white space between HTML tags, while
Mozilla does.

For example:
<td id=foo>
<b>bar</b>
</td>

In this example, IE will say foo has one childNode and Mozilla will say
it has three.

For another example:
<td id=foo><b>bar</b></td>

Both browsers will interpret this example identically.
For the record, replacing the innerHTML is identical to removing the
node and then creating a new one, except that the browser has to parse
the new innerHTML and do its own node-building.

I don't think you'll have a problem with either method, except that
older browsers may not understand your attempts to do the node-building
for them.

Jul 23 '05 #4
coolsti wrote:
Can someone tell me how to do this if it is possible?

I have a table based web site, and I would like to dynamically change the
text that is shown in a particular cell of a table. I give the cell the
unique ID label1 so I can get a hold of it, and I can get a javascript to
display the data in an alert box (this is just to see if I can access the
text) with:

mycel = document.getEle mentById('label 1');
myceltext = mycel.childNode s.item(0);
currenttext = myceltext.data;
alert(currentte xt);

But how do I exchange the text in this cell for something else?

Thanks for any help!

steve


function replaceText(sId , sText)
{
var el;
if (document.getEl ementById
&& (el = document.getEle mentById(sId)))
{
while (el.hasChildNod es())
el.removeChild( el.lastChild);
el.appendChild( document.create TextNode(sText) );
}
}

Jul 23 '05 #5
On Wed, 01 Jun 2005 08:29:06 -0700, RobB wrote:
coolsti wrote:
Can someone tell me how to do this if it is possible?

I have a table based web site, and I would like to dynamically change
the text that is shown in a particular cell of a table. I give the cell
the unique ID label1 so I can get a hold of it, and I can get a
javascript to display the data in an alert box (this is just to see if
I can access the text) with:

mycel = document.getEle mentById('label 1'); myceltext =
mycel.childNode s.item(0); currenttext = myceltext.data;
alert(currentte xt);

But how do I exchange the text in this cell for something else?

Thanks for any help!

steve


function replaceText(sId , sText)
{
var el;
if (document.getEl ementById
&& (el = document.getEle mentById(sId))) {
while (el.hasChildNod es())
el.removeChild( el.lastChild);
el.appendChild( document.create TextNode(sText) );
}
}


Hi and thanks! A better idea than what I came up with (using a while loop
to remove all child nodes and a check to see if document.getEle mentById
exists).

-steve
Jul 23 '05 #6

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

Similar topics

1
3776
by: Michael | last post by:
How can I use onclick to change a table cell background color. I know I can use this.className=? to change the classname. But then I would have to put the code into every single table cell. I read once about using a collection method. Lets say a table has 100 TD Cells with a white background. I want to click on just 1 and then have it change to a Yellow background. Then when I click on a different Cell have the old one change back to...
6
7680
by: chris | last post by:
is there a way by using html or javascript or anything else for that matter to detirmine the actual height or width of a table or cell for example if i only set the height to 100 and the data in the cell needs a higher cell it expands to suit - i want to find out what the actual height ends up ?? thanks for your help chris
5
4894
by: john_williams_800 | last post by:
Hi; I am just starting to use the DOM to do some more advanced javascripting so please be patient with my question if it is an ignorant question. I would like to use the DOM to dynamically create an html table via javascript. While that table is being dynamically created in a javascript function I would like to dynamically insert text and a hyperlink with an image into each cell.
4
4130
by: brian | last post by:
Hello, I have spen hours trying to find information on dynamically building a table from an array. What I need: I have an array pulling file paths from a directory using a For Next Loop. F is the file. For every instance of F I would like the file path put into the table. The table is just a single column table. I have looked at the Table class on msdn and that hasn't
1
6722
by: Tracey | last post by:
Hi, there My application requires to display multiline text in the cell of datagrid. I set the preferredheight of datagrid's tablsstyle, however, it is fixed. For text longer than that, the rest of text is cut missing. Can someone tell me how to do that? thanks.
0
1273
by: Engineerik | last post by:
I have looked at the syncfusion datagridformattabletextboccolumn sample but I have been unable to figure out how to base the special formatting on whether the datasource for the cell has a changed value. Ideally I would like to bold text for a cell if the datarowstate of the underlying datarow is anything other than unchanged. I know there must be a way but I haven't been able to do it. Anybody know how to do this?
6
10004
by: anirban.anirbanju | last post by:
hi there, i've some serious problem to add rows dynamically in a table. my table contains 5 cell. | check | from_value | to_value | color_text | color_value | --------------------------------------------------------------------------------- | | | | | | | | | | |
1
1333
by: sureshreddy | last post by:
Hi friends, I am working on project in which i need a color change for the selected cell when i check the checkbox. when i uncheck it the color should also disappear. If you didn't understand what i said plz go to www.clickjobs.com and check a checkbox and you can undertand. If possible plz send me the code as it is urgent. Thanks in advance SureshReddy
3
1371
by: andersond | last post by:
I have a table named 'tableCompletionFeedback' with a cell named 'percent'. In the cell I have placed text ('10% complete") and I want to be able to change the text with javascript. My problem is I don't know how to address the text in that cell. This, for example, doesn't work: document.getElementById('percent').text="20% complete"; Can anyone tell me how to make the text change with javascript?
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.