469,306 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

how to write to a table cell??

Ben
Hi all,

Just wondering how to write (using document.write) to a table cell. I
have table with 3 rows and 3 colums. I want to write from within the
Javascript to say third column of a first row. How do I do it?

I think one way is to write the entire table out in
document.write("<table>...) but I'd like to know if it is possible to
access a particular cell in a table.

If you know of any good online tutorial, could u send me a link?

Thanks!
Ben
Jul 23 '05 #1
8 17142
mps
I once used the reference:

tableID.rows[x].cells[y].innerHTML

Good luck.
Marco
"Ben" <cr*********@yahoo.com> schreef in bericht
news:d9**************************@posting.google.c om...
Hi all,

Just wondering how to write (using document.write) to a table cell. I
have table with 3 rows and 3 colums. I want to write from within the
Javascript to say third column of a first row. How do I do it?

I think one way is to write the entire table out in
document.write("<table>...) but I'd like to know if it is possible to
access a particular cell in a table.

If you know of any good online tutorial, could u send me a link?

Thanks!
Ben

Jul 23 '05 #2
In article <d9**************************@posting.google.com >,
cr*********@yahoo.com enlightened us with...
Hi all,

Just wondering how to write (using document.write)


Why? That's a bad way of doing it that should only be used if you have
to support very old browsers like NN4.

If you only have to support newer browsers (IE5+/NN6+/Mozilla/Opera),
you should be using DOM methods to change the content of table cells.

I can post code to change the content of table cells using DOM methods
if you'd like.

You can't change one cell only using document.write AFAIR. You'd have to
put the table in a layer and change the whole thing. I haven't had to do
that in quite some time, so I could be wrong, but IIRC, that's what I
had to do.

--
--
~kaeli~
You can't have everything. Where would you put it?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #3
> I think one way is to write the entire table out in
document.write("<table>...) but I'd like to know if it is possible to
access a particular cell in a table.


Anything in the body (and even most tags outside) can be written by JS
using document.write(). Just add <script>document.write("Any html you
so desire")</script> in your html wherever you wish to write it.

Suggestion: Add to all your documents or master/general/core js file
(if you use one)- function dw(s){document.write(s)}. This will save
you alot of characters with multiple writes saving 11 characters be dw
instance.

JsD
Jul 23 '05 #4
Ben
kaeli <ti******@NOSPAM.comcast.net> wrote in message news:<MP************************@nntp.lucent.com>. ..
In article <d9**************************@posting.google.com >,
cr*********@yahoo.com enlightened us with...
Hi all,

Just wondering how to write (using document.write)


Why? That's a bad way of doing it that should only be used if you have
to support very old browsers like NN4.

If you only have to support newer browsers (IE5+/NN6+/Mozilla/Opera),
you should be using DOM methods to change the content of table cells.

I can post code to change the content of table cells using DOM methods
if you'd like.

Can you show me an example??

Thanx
Ben
Jul 23 '05 #5
In article <d9**************************@posting.google.com >,
cr*********@yahoo.com enlightened us with...

I can post code to change the content of table cells using DOM methods
if you'd like.

Can you show me an example??


(jeez but this was harder to figure out cross-browser than just
dynamically creating a whole table...)
Okay, tested in NN7.2, Mozilla 1.2, Opera 7.23, and IE6.0.28
successfully. Make no assumptions about support in other browsers
without testing them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Changing table content dynamically with DOM methods</title>
<script type="text/javascript">
function changeContent(frm)
{
if (! document.getElementById)
{
alert("Sorry, your browser doesn't support this.");
return;
}

/* check to make sure we have everything we need. */
var R = frm.elements["row"].value;
var C = frm.elements["col"].value;
var newVal = frm.elements["newVal"].value;
if (!R || isNaN(R))
{
alert("Row must be a number.");
return;
}
if (!C || isNaN(C))
{
alert("Column must be a number.");
return;
}
if (! newVal || newVal == "")
{
alert("You forgot to give a new value.");
return;
}

/* row and column indexes are 0 based, so subtract one from the
logical values of 1-4 to make it 0-3 */
R--;
C--;

/* for this example, we are using just plain text in the table cells
- this gets a little more complicated if other things are in the table
cells or if the text length is long enough to span multiple nodes */
var row = document.getElementById("tb").getElementsByTagName ("tr")
[R];
var cell = row.getElementsByTagName("td")[C];
var newCell = document.createElement("td");
row.replaceChild(newCell, cell);
newCell.appendChild(document.createTextNode(newVal ));
return;
}
</script>
</head>

<body>
<p>A 4 by 4 table...</p>
<table id="t1" border="1" cellpadding="5" cellspacing="0">
<tbody id="tb">
<tr>
<td><p>R1 C1</p></td>
<td><p>R1 C2</p></td>
<td><p>R1 C3</p></td>
<td><p>R1 C4</p></td>
</tr>
<tr>
<td><p>R2 C1</p></td>
<td><p>R2 C2</p></td>
<td><p>R2 C3</p></td>
<td><p>R2 C4</p></td>
</tr>
<tr>
<td><p>R3 C1</p></td>
<td><p>R3 C2</p></td>
<td><p>R3 C3</p></td>
<td><p>R3 C4</p></td>
</tr>
<tr>
<td><p>R4 C1</p></td>
<td><p>R4 C2</p></td>
<td><p>R4 C3</p></td>
<td><p>R4 C4</p></td>
</tr>
</tbody>
</table>
<form id="f1" name="f1">
<p>Replace which cell?</p>
<p>
Row (1-4): <input type="text" name="row" size="3"><br>
Column (1-4): <input type="text" name="col" size="3">
</p>
<p>New value: <input type="text" name="newVal"></p>
<input type="button" name="b1" value="Try It" onClick="changeContent
(this.form)">
</form>
</body>
</html>

--
--
~kaeli~
It was recently discovered that research causes cancer in
rats.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #6
JRS: In article <MP************************@nntp.lucent.com>, dated
Tue, 20 Jul 2004 10:47:01, seen in news:comp.lang.javascript, kaeli
<ti******@NOSPAM.comcast.net> posted :
/* check to make sure we have everything we need. */
var R = frm.elements["row"].value;
var C = frm.elements["col"].value;
var newVal = frm.elements["newVal"].value;
if (!R || isNaN(R))
{
alert("Row must be a number.");
return;
}


In practice, Row must be a positive integer; and one can set an upper
limit on the number of digits it needs.

So if (!/^\d{1,3}$/.test(R)) // E&OE

seems a better test.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
Java script Dude wrote:

Who wrote this? <http://netmeister.org/news/learn2quote.html>
vvvvvvvvvvvvvvv
I think one way is to write the entire table out in
document.write("<table>...) but I'd like to know if it is possible to
access a particular cell in a table.
Anything in the body (and even most tags outside) can be written by JS
using document.write(). Just add <script>document.write("Any html you
so desire")</script> in your html wherever you wish to write it.


It should be at least

<script type="text/javascript">
document.write("Any html you so desire");
</script>
Suggestion: Add to all your documents or master/general/core js file
(if you use one)- function dw(s){document.write(s)}. This will save
you alot of characters with multiple writes saving 11 characters be
dw instance.


var dw = document.write;

would be better. But since document.write() is a host method it should
be something like

var dw;
if (this.document && document.write)
{
dw = document.write;
}
else
{
dw = function() {}
}

(in the global execution context).
PointedEars
Jul 23 '05 #8
Thomas 'PointedEars' Lahn wrote:
Java script Dude wrote:

Who wrote this? <http://netmeister.org/news/learn2quote.html>
vvvvvvvvvvvvvvv
I think one way is to write the entire table out in
document.write("<table>...) but I'd like to know if it is possible to
access a particular cell in a table.


Anything in the body (and even most tags outside) can be written by JS
using document.write(). Just add <script>document.write("Any html you
so desire")</script> in your html wherever you wish to write it.


It should be at least

<script type="text/javascript">
document.write("Any html you so desire");
</script>
Suggestion: Add to all your documents or master/general/core js file
(if you use one)- function dw(s){document.write(s)}. This will save
you alot of characters with multiple writes saving 11 characters be
dw instance.


var dw = document.write;

would be better. But since document.write() is a host method it should
be something like

var dw;
if (this.document && document.write)
{
dw = document.write;
}
else
{
dw = function() {}
}

(in the global execution context).


I thought I remembered some issues with assigning document.write to an
instance variable and attempting to use it to output text, so I did a small
test:

<script type="text/javascript">
var dw;
if (this.document && document.write) {
dw = document.write;
} else {
dw = function() {}
}
dw("This is a test");
</script>

IE6SP1: works as expected

Mozilla 1.7.1: Error: uncaught exception: [Exception... "Illegal operation
on WrappedNative prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame ::
file:///C:/DOCUME~1/grantw/LOCALS~1/Temp/hs~new.htm :: <TOP_LEVEL> :: line
16" data: no]

Netscape 4.78: JavaScript Error: Document.prototype.write called on
incompatible Window

Firefox 0.9.2: Error: uncaught exception: [Exception... "Illegal operation
on WrappedNative prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS frame ::
file:///C:/DOCUME~1/grantw/LOCALS~1/Temp/hs~new.htm :: <TOP_LEVEL> :: line
20" data: no]

Opera 6.05 & 7.53: produce no output, generate no JavaScript error

Given the lack of support for the approach shown above in some of the most
modern browsers, I would say it's probably not recommened.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jim Madsen | last post: by
2 posts views Thread by Marian Aldenhövel | last post: by
6 posts views Thread by Anon | last post: by
1 post views Thread by Thanks | last post: by
1 post views Thread by Bart Lateur | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.