Modify table cell...

A

Anon

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.
 
I

Ivo

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
 
R

RobG

Anon said:
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.
 
A

Anon

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. ;-)
 
M

Mick White

Anon said:
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
 
A

Anon

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.
 
E

Evertjan.

Anon wrote on 12 mrt 2005 in comp.lang.javascript:
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[]?
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top