473,605 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17895
mps
I once used the reference:

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

Good luck.
Marco
"Ben" <cr*********@ya hoo.com> schreef in bericht
news:d9******** *************** ***@posting.goo gle.com...
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*********@yah oo.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>documen t.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******@NOSPA M.comcast.net> wrote in message news:<MP******* *************** **@nntp.lucent. com>...
In article <d9************ **************@ posting.google. com>,
cr*********@yah oo.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*********@yah oo.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>Changi ng table content dynamically with DOM methods</title>
<script type="text/javascript">
function changeContent(f rm)
{
if (! document.getEle mentById)
{
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.getEle mentById("tb"). getElementsByTa gName("tr")
[R];
var cell = row.getElements ByTagName("td")[C];
var newCell = document.create Element("td");
row.replaceChil d(newCell, cell);
newCell.appendC hild(document.c reateTextNode(n ewVal));
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="change Content
(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************ ************@nn tp.lucent.com>, dated
Tue, 20 Jul 2004 10:47:01, seen in news:comp.lang. javascript, kaeli
<ti******@NOSPA M.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.htm l>
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>documen t.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.htm l>
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>documen t.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_B AD_OP_ON_WN_PRO TO)" 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.protot ype.write called on
incompatible Window

Firefox 0.9.2: Error: uncaught exception: [Exception... "Illegal operation
on WrappedNative prototype object" nsresult: "0x8057000c
(NS_ERROR_XPC_B AD_OP_ON_WN_PRO TO)" 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*****@agrico reunited.com>
comp.lang.javas cript 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
13711
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 would like to try to write a script so that the background color in a cell in a table changes color (between two or three specified colors)). How do I get at the background color property in a table cell? Sorry for such a basic question.
2
4179
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 export spreadsheets from Excel to HTML. The resulting HTML is horrible to
1
2447
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 codes are as follows; please read comments on the code: index.html ---------- <html>
6
4987
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 + '"></TD>" ); How can I programmatically modify the contents of the cell whose name is held within m_UniqueCellName? The variable will get passed around to other functions, but try as I might,
1
3539
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 black for example). I am dynamically creating the LinkButton controls and adding them into the table cell and I've also hooked up an event handler for each LinkButton's Click event. This all works fine. Now in the Link Button's Click event...
1
16529
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 parent element, and position: absolute; bottom: 0px;
5
4432
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 table havind ID on a concerned cell (initially blank). The div also has a command button. Now once the button is cliked, a popup window should open and the concerned cell (present in the main window) should be populated with a value. code to...
2
1503
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 I try with document.getElementById("respective id given by me").write, it gives Error on page. I'm bit puzzled. Can anyone of you help me.
0
8009
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7938
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8078
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6752
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5892
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3914
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2441
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.