javascript insertRow doesn't work ;-(

M

milkyway

Hello,

I am working with an old Java Script code and have found the following
but after putting in alerts I have found that insertRow doesn't work.

What is the problem?

TIA

function addRowDOM (tableID) {
// pass every cell content as a futher arg
alert("here");
var table =
document.all ? document.all[tableID] :
document.getElementById(tableID);
alert("here 2");
if (arguments.length > 1) {
alert(table.rows.length);
var row = table.insertRow(table.rows.length);
alert("after var");
if (document.all) {
alert("second if");
for (var i = 1; i < arguments.length; i++) {
alert("in the for loop");
var cell = row.insertCell(i - 1);
cell.innerHTML = arguments;
}
}
}
 
R

RobG

milkyway said:
Hello,

I am working with an old Java Script code and have found the following
but after putting in alerts I have found that insertRow doesn't work.

What is the problem?

You were missing a single closing brace.

Others may wish to pitch in, but it seems to me that insertRow and
insertCell are not part of the W3C DOM interface. As far as I can
tell, they are a legacy of Netscape (3? 4?) that is also implemented by
IE and sometimes referred to as the Table Object Model (TOM).

There is only one interface: the DOM. Whilst Mozilla et al still
support some of the older methods (sometimes called "specification
level 0") you should use those of the applicable standard.

The W3C reference is here:


<URL:http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1590626201>

or for Firefox/Mozilla in a simplified form that includes legacy stuff,
try here:

<URL:http://www.mozilla.org/docs/dom/domref/>

As an example, I've included a modified version of your code below. It
does minimal feature checking and works in the 'zillas and IE 6.

Note that if you don't include the <tbody> element and append your rows
to the <table>, then Firefox appends them to the table, not the tbody
element. If no tbody is in the HTML, Firefox will include it in its
DOM at runtime, but the rows are still added to the table, not the
tbody. Whilst this may be strictly correct, it is a bit counter
intuitive. Therefore it's probably best to include a tbody element in
the HTML and append to that to prevent irregularities. A table can
have multiple tbody elements.

insertRow seems to always add to the tBody, whether one is in the HTML
or not.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>Insert Row</title>
<script type="text/javascript">
function addRowDOM0(tableID) {
// pass every cell content as a futher arg
var table = document.all ? document.all[tableID] :
document.getElementById(tableID);
if (arguments.length > 1) {
var row = table.insertRow(table.rows.length);
for (var i = 1; i < arguments.length; i++) {
var cell = row.insertCell(i - 1);
cell.innerHTML = arguments;
}
}
}

function addRowDOM1(tRef){
var oTable = document.getElementById(tRef);
if (oTable && document.createElement) {
var oRow = document.createElement('TR');
for (var i=1; i<arguments.length; i++) {
var oCell = document.createElement('TD');
var oTxt = document.createTextNode(arguments);
oCell.appendChild(oTxt);
oRow.appendChild(oCell);
}
oTable.appendChild(oRow);
}
}
</script>
</head>
<body>
<table><tbody id="fred">
<tr>
<td onclick="
addRowDOM0('fred','old way 1','old way 2');
">do old way</td>
<td onclick="
addRowDOM1('fred','new way 1','new way 2');
">New way</td>
</tr>
</tbody></table>
</body>
</html>
 
M

Michael Winter

[snip]

That's *one* of the specifications. Unlike DOM 1, DOM 2 (and all
subsequent versions) is split into modules, one of which is DOM 2 HTML. In
there you will find objects and methods specific to HTML documents,
including those for the manipulation of tables. See
<URL:http://www.w3.org/DOM/DOMTR.html#DOML2>.

By the way, I believe it best to refer to the undated URLs unless you're
referencing a specific document. So, for example, instead of

<URL:http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109>

use

<URL:http://www.w3.org/TR/DOM-Level-2-HTML>

This latter form is listed as the "Latest version" at the beginning of
most (all?) W3C technical reports.

[snip]
insertRow seems to always add to the tBody, whether one is in the
HTML or not.

By design. See
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>.

[snip]

Mike
 
R

RobG

Michael Winter wrote:
[...]
<URL:http://www.w3.org/TR/DOM-Level-2-HTML>

This latter form is listed as the "Latest version" at the beginning of
most (all?) W3C technical reports.

[snip]
insertRow seems to always add to the tBody, whether one is in the
HTML or not.


By design. See
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-39872903>.

Thanks Mike. I find navigating around the W3C site painful, but you've
given me the impetus to tidy my bookmarks once and for all.
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top