Forum Moderators: open

Message Too Old, No Replies

Dynamically write value to table cell

how do I?

         

Scally_Ally

3:25 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



Hi there,

I want to be able to write the value of what the user inserts in a text box to a table cell.. I know it sounds wierd but there is a bigger picture here.

At the moment all i can do is write what the user inserts into another textbox..

Here is my function. (where TheText is the original text and Text2 is the duplicated text)

function duplicateText()
{
document.TheForm.Text2.value = document.TheForm.TheText.value;
}

<body onkeyup="duplicateText();">

This copys what the user write from one box to another.

How do i copy this text to a table cell?
Do i use the getElementByID function.

Any help greatly appreciated.

Ally

ChadSEO

3:48 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



Ally,

You're definitely on the right track. First you'll want to assign an id to the table cell that you want to change. Then on the input field, you can do something like this:

<input type="text" onkeyup="document.getElementById('test').innerHTML=this.value;">

Replace "test" with whatever id you gave your table cell, and it should update it on the fly. Note that this is case-sensitive, and "innerHTML" doesn't follow normal javascript capitlization.

Chad

RonPK

3:51 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[Never mind, ChadSEO was way faster than me]

ChadSEO

3:58 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



RonPK,

Haha, half the time I have to ask myself, not "Can I answer this question", but "Can I answer this question faster than anyone else?" :)

Chad

Scally_Ally

4:04 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



Thanks guys...

As a follow up could i use the getElementsbyTags function?

If not what is the difference?

Ally

RonPK

4:32 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



element.getElementsByTagName('tagName') returns an array with all the elements tagName in element.

Are you still with me ;)

var allTDs = document.getElementsByTagName('td');

allTDs is now an array containing all the TDs in the document. As with all arrays, you can loop through it or address individual items, e.g. allTDs[5].

Scally_Ally

7:48 am on Aug 19, 2005 (gmt 0)

10+ Year Member



cool, so i presume that then instead of using an id name to access the table cell i could use say allTDs[3] for the fourth element that appears on the page.

Thanks loads

Ally