473,403 Members | 2,359 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,403 software developers and data experts.

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 17841
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Madsen | last post by:
I am trying to learn JavaScript--(complete newbie--and don't know C or C++). I've been reading "The Book of JavaScript" by Thau. I don't see anything about table properties in the book. (I...
2
by: Marian Aldenhövel | last post by:
Hi, Short question: Is it possible to write a CSS-Style that formats <INPUT TYPE=TEXT>-Elements in Table-Cells in a way that they use all the horizontal space available? Long question: I...
1
by: Ben | last post by:
Hi all, I'm trying to write inside a table cell from external javascript but am not successful. When I insert inside a form within <td...>, it works but does not work for normal table cell. My...
6
by: Anon | last post by:
I have a table with a cell. The cell's ID is created using a unique name that is held in m_UniqueCellName and the cell is created like so... document.write( "<TD ID="' + m_UniqueCellName +...
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
1
by: Bart Lateur | last post by:
I'm trying to put a utton at the bottom (right) of a TD cell, irrespective of what else is in there. Usually, with other HTML block elements, we're told to use position: relative on the...
5
by: prawasini | last post by:
hi, I have a code where a value in one of a table cell needs to be populated on a command button click event. Scenario: There is a main window having multiple <DIV>s In one of the Div there is a...
2
by: Vasu | last post by:
Hi! Could anybody help me guide how to write something in between <td></ tdtags. When I try to write through document.write, it wipes the whole table and writes on to a blank document, but when...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
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...
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...
0
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,...

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.