By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,769 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,769 IT Pros & Developers. It's quick & easy.

Modify table cell...

P: n/a
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,

myid = document.getElementById( m_UniqueCellName );

document.all.myid.innerHTML = "Something!";

simply doesn't work. What is it that I'm missing? Any help would be
greatly appreciated.
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ivo
"Anon" wrote
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,

myid = document.getElementById( m_UniqueCellName );

document.all.myid.innerHTML = "Something!";

simply doesn't work.


You made myid reference to the object itself, not to its name, which is what
document.all expects. Try:

myid.innerHTML="Yes!";
or
document.all[ m_UniqueCellName ].innerHTML = "Something!";

Note the dot following the closing ] and the absence of one preceding the [.
The first option is the prefered one as document.all is not part of any
standard (neither is innerHTML btw).

hth
--
Ivo
http://4umi.com/
Jul 23 '05 #2

P: n/a
Anon wrote:
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,

myid = document.getElementById( m_UniqueCellName );

document.all.myid.innerHTML = "Something!";

simply doesn't work. What is it that I'm missing? Any help would be
greatly appreciated.


When you create m_UniqueCellName, it is just a string that has
no special properties.

When you use document.write, you have created HTML that is
parsed by the browser and rendered in a browser window. You
never created a reference to the cell you create, hence you
don't have one to 'remember'. m_UniqueCellName will only refer
to the original variable which contains a string.

If you want to create the cell in a manner that allows you to
reference it later, use DOM methods to build your table. Or,
if you create m_UniqueCellName as global variable (not recommended),
you can later get a reference to the cell using:
if (document.getElementById) {
var myid = document.getElementById(m_UniqueCellName);
} else if (document.all) {
var mtid = document.all(m_UniqueCellName);
}

myid.innerHTML = 'something!';

As noted elsewhere, innerHTML is a Microosft invention that is
not part of any W3C standard, it should be used judiciously.
If you want to manipulate the contents of a table cell you
should use DOM methods, however if all that is required is the
simple replacement of some text in the cell, it is fine in most
cases.

--
Rob

Jul 23 '05 #3

P: n/a

"Ivo" <no@thank.you> wrote in message
news:42*********************@news.wanadoo.nl...
You made myid reference to the object itself, not to its name, which is
what
document.all expects. Try:

myid.innerHTML="Yes!";
or
document.all[ m_UniqueCellName ].innerHTML = "Something!";


Thanks Ivo, the second method works perfectly, however I'd feel more
comfortable if I could get the first method working too. ;-)
Jul 23 '05 #4

P: n/a
Anon wrote:
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>" );

document.write( "<TD ID='" + m_UniqueCellName + "'></TD>" );

Mick
Jul 23 '05 #5

P: n/a

"RobG" <rg***@iinet.net.au> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
As noted elsewhere, innerHTML is a Microosft invention that is
not part of any W3C standard, it should be used judiciously.
If you want to manipulate the contents of a table cell you
should use DOM methods, however if all that is required is the
simple replacement of some text in the cell, it is fine in most
cases.


I used the DOM methods to create a Table, then added a row like so,

var cell = document.createElement( "TD" );
cell.className = m_UniqueCellName ;
cell.appendChild( document.createTextNode( "Some Cell" ) );

However, the following code only works in IE and Opera, but not NN
or Firefox (all browsers are the latest version):

m_UniqueCellName.document.write( "hello" );

But there's also the added effect that (in IE and Opera), while the cell
contents are indeed changed, the entire contents of the page are erased.
Jul 23 '05 #6

P: n/a
Anon wrote on 12 mrt 2005 in comp.lang.javascript:

"RobG" <rg***@iinet.net.au> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
As noted elsewhere, innerHTML is a Microosft invention that is
not part of any W3C standard, it should be used judiciously.
If you want to manipulate the contents of a table cell you
should use DOM methods, however if all that is required is the
simple replacement of some text in the cell, it is fine in most
cases.
I used the DOM methods to create a Table, then added a row like so,

var cell = document.createElement( "TD" );
cell.className = m_UniqueCellName ;


m_UniqueCellName returns a string? Or is it a function?
cell.appendChild( document.createTextNode( "Some Cell" ) );

However, the following code only works in IE and Opera, but not NN
or Firefox (all browsers are the latest version):
You cannot use a class [className] as a name or an id

You should not use a name as an id, and let the name be used as an
object variable, when wanting to be somewhat cross browser compatible.

You should use an id and use getElementById() to be somewhat cross
browser compatible.
m_UniqueCellName.document.write( "hello" );
m_UniqueCellName.document.write()

The cell [that is referenced incorrectly, see above,] is NOT a child of
the document. Your code is nonsensical. Review in your mind the DOM tree.

But there's also the added effect that (in IE and Opera), while the
cell contents are indeed changed, the entire contents of the page are
erased.


A logical consequence of using document.write() in a finished page.
Then document.write() calls document.open() first.

Is m_UniqueCellName perhaps an alias of window[]?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.