473,396 Members | 1,827 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,396 software developers and data experts.

writing to a give table element

mxa
Hi,
given table :
<table id='t1'>
<tr><td> data1</td><td>data2</td></tr>
</table>

i would like to write a function to write and change data2
( location is fixed ).

function changeme () {

document.write('new data2');

}

how can I focus on 'data2' before calling the above function?
thanks
Michael

Jul 23 '05 #1
5 1445
mx*@yahoo.com wrote:
Hi,
given table :
<table id='t1'>
<tr><td> data1</td><td>data2</td></tr>
</table>

i would like to write a function to write and change data2
( location is fixed ).

function changeme () {

document.write('new data2');

}

how can I focus on 'data2' before calling the above function?
thanks
Michael


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript">
//<![CDATA[

function changeme()
{
var tbl = document.getElementById('t1'); //table
var firstrow = tbl.rows.item(0); //row 1 (DOM: 0)
var cell2 = firstrow.cells.item(1); //2nd TD
while (cell2.hasChildNodes()) //anything in there?
cell2.removeChild(cell2.lastChild); //adios
cell2.appendChild(document.createTextNode('new data2')); //new text
node, add
}

onload = function()
{
setTimeout(changeme, 3000);
}

//]]>
</script>
</head>
<body>
<table id="t1" cellspacing="10">
<tbody style="font:32px tahoma;">
<tr>
<td>data1</td>
<td>data2</td>
</tr>
</tbody>
</table>
</body>
</html>

http://www.sitepoint.com/article/rough-guide-dom

Jul 23 '05 #2
Just a note: could also go -

var tbody =
document.getElementById('t1').getElementsByTagName ('tbody').item(0);
var firstrow = tbody.rows.item(0);
Another ref:

http://www.brainjar.com/dhtml/intro/

Jul 23 '05 #3
RobB wrote:
[...]

Now there's an easy way and a hard way...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Change Data2</title>

<script type="text/javascript">
function changeme(ele,str) {
var tbl = document.getElementById(ele);
t1.innerHTML = str;
}
</script>

</head>
<body>
<table cellspacing="10">
<tbody style="font:32px tahoma;">
<tr>
<td>data1</td>
<td id="t1">data2</td>
</tr>
</tbody>
</table>

<div onclick="changeme('t1','Here is the new text');" style="
font-family: tahoma;
border: 1px solid blue;
background-color: #ddddee;
width: 20em;">Click here to to change the text</div>
</body>
</html>

I reckon that's the easy way! :-)

Of course, getElementsById is not compatible with older browsers and
innerHTML, whilst well supported, is not part of the official DOM. So
if this is for real web use, let us know and we'll post something a bit
more robust (but it'll take a few more lines of code...)

--
Fred
Jul 23 '05 #4
Fred Oz wrote:
RobB wrote: [...] <script type="text/javascript">
function changeme(ele,str) {
var tbl = document.getElementById(ele);
t1.innerHTML = str;
}
</script>


Believe it or not, the above rubbish worked in Safari. My apologies,
the correct code is below.

<script type="text/javascript">
function changeme(ele,str) {
var d = document.getElementById(ele);
d.innerHTML = str;
}
</script>

--
Fred
Jul 23 '05 #5
Fred Oz wrote:
Fred Oz wrote:
RobB wrote: [...]
<script type="text/javascript">
function changeme(ele,str) {
var tbl = document.getElementById(ele);
t1.innerHTML = str;
}
</script>


Believe it or not, the above rubbish worked in Safari. My

apologies, the correct code is below.

<script type="text/javascript">
function changeme(ele,str) {
var d = document.getElementById(ele);
d.innerHTML = str;
}
</script>

--
Fred


Fred:
some leisure reading.

http://www.developer-x.com/content/innerhtml/

Jul 23 '05 #6

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

Similar topics

4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
6
by: Christopher Benson-Manica | last post by:
I have some markup like the following: <form> <table> <script> <!-- Write the table markup //--> </script> </table> <form>
0
by: Chris Birmele | last post by:
I am having difficulties maintaining table relationships when writing XML files, despite having set the relations property NESTED to TRUE. I assume therefore that something is wrong with my code. ...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
4
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int...
2
by: ezmiller | last post by:
Hi, I have some code (which I will paste in below) that writes out some HTML dynamically using the W3C DOM...the last part of the code write out a simple table. MY problem is that the table is...
7
by: slitvinov | last post by:
I am learning Relax NG. The problem is that I cannot figure out how to make a schema for a table. In my case I would like to make a table with any name of child elements (columns) but columns...
118
by: Chuck Cheeze | last post by:
This might be in the wrong group, but... Here is an example of my data: entry_id cat_id 1 20 2 25 3 30 4 25 5 35
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.