how to write to a table cell??

B

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
 
K

kaeli

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.

--
 
J

Java script Dude

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
 
B

Ben

kaeli said:
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
 
K

kaeli

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 20 Jul 2004 10:47:01, seen in kaeli
/* 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.
 
T

Thomas 'PointedEars' Lahn

Java script Dude wrote:

Who wrote this? said:
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");
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
 
G

Grant Wagner

Thomas said:
Java script Dude wrote:



It should be at least

<script type="text/javascript">
document.write("Any html you so desire");


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top