473,881 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript insertRow doesn't work ;-(

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.getEle mentById(tableI D);
alert("here 2");
if (arguments.leng th > 1) {
alert(table.row s.length);
var row = table.insertRow (table.rows.len gth);
alert("after var");
if (document.all) {
alert("second if");
for (var i = 1; i < arguments.lengt h; i++) {
alert("in the for loop");
var cell = row.insertCell( i - 1);
cell.innerHTML = arguments[i];
}
}
}

Jul 23 '05 #1
3 16207
milkyway wrote:
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 "specificat ion
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>In sert Row</title>
<script type="text/javascript">
function addRowDOM0(tabl eID) {
// pass every cell content as a futher arg
var table = document.all ? document.all[tableID] :
document.getEle mentById(tableI D);
if (arguments.leng th > 1) {
var row = table.insertRow (table.rows.len gth);
for (var i = 1; i < arguments.lengt h; i++) {
var cell = row.insertCell( i - 1);
cell.innerHTML = arguments[i];
}
}
}

function addRowDOM1(tRef ){
var oTable = document.getEle mentById(tRef);
if (oTable && document.create Element) {
var oRow = document.create Element('TR');
for (var i=1; i<arguments.len gth; i++) {
var oCell = document.create Element('TD');
var oTxt = document.create TextNode(argume nts[i]);
oCell.appendChi ld(oTxt);
oRow.appendChil d(oCell);
}
oTable.appendCh ild(oRow);
}
}
</script>
</head>
<body>
<table><tbody id="fred">
<tr>
<td onclick="
addRowDOM0('fre d','old way 1','old way 2');
">do old way</td>
<td onclick="
addRowDOM1('fre d','new way 1','new way 2');
">New way</td>
</tr>
</tbody></table>
</body>
</html>

--
Rob
Jul 23 '05 #2
On Tue, 07 Dec 2004 23:57:34 GMT, RobG <rg***@iinet.ne t.auau> wrote:

[snip]
The W3C reference is here:

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

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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
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.

--
Rob
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2211
by: milkyway | last post by:
Hello, Just new to all of this. The code below is supposed to load an XML file for use in a table. It doesn't work because of one command: table.insertRow command: I have seen the specs saying that it should be a -1 or the length of the table. I have tried.
4
11961
by: rick | last post by:
The following basic script works fine in firefox by not in IE. Can anyone spot the problem? In IE I can only delete the first line but not the lines created by javascript. Also, look at the HTML code for the first line (click the Table HTML button:)) you will fine that the code displayed is not the same as was written. "onChange" was changed to "onchange" etc. Please help. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">...
1
3187
by: putty | last post by:
I found a few posts of people asking about insertCell()/insertRow() not working in IE6 SP2, and a few others about getting "null is null or not an object" errors, but no one posted a solution anywhere or noted this tiny yet curious change in SP2.. so here it is: In IE6 SP2 rows must be inserted into a tBody instead of a Table directly, after that you may insert TDs into TRs any way you wish. insertRow() and insertCell() work the same as...
2
9550
by: Howard Jess | last post by:
CLJ -- I've searched the newsgroup and FAQ for info on insertRow(), but didn't see this reported. It seems that Internet Explorer doesn't respond correctly to either insertRow() or insertCell(), if the object that calls this method isn't already within a table. That is: row = tbody.insertRow(0); // fails if tbody's parent is null
8
3470
by: Hal Burgiss | last post by:
Hi, and help for a novice trying to get some data dynamically into tables. I did a small proof of concept page which works fine in IE6 and Mozilla. Using code such as this: tbody = document.getElementById('tb'); thead = document.getElementById('header'); tr = thead.insertRow(-1); td = document.createElement('th'); tr.appendChild(td);
5
12323
by: fjleon | last post by:
Hi, i want to create a table declared inside a <td> , which already exists as follows: <tr> <td id="prueba" colspan="4"> </td> </tr> I have a button somewhere calling this script, which i have reduced as much for this post:
1
15794
by: raju78.k | last post by:
Hi, I have a problem with FireFox. I have written a function to Add rows without submiting the form. This function works fine in IE, but not in FireFox. The function is : function createRows(deviceId,deviceType,modelName,ipAddress,macAddress,imageURL)
2
1881
by: prasath03 | last post by:
Dear Members, I am going to create the online form creation in html with javascript, i have a problem with the following code, i got this code from internet, the following script only works in IE but it doesn't work in other browser like (Firefox and Opera), i don't know what's the problem with code. If anybody know the solution let me know... <html> <head> <title>Adding and Removing Rows</title> <script language="javascript">
1
3268
by: kanishka1213 | last post by:
#!/activeperl/perl/bin/perl use CGI qw(:standard); print "Content-type: text/html\n\n"; print <<EndOfHTML; <html> <head> <script language="javascript" type="text/javascript">
0
9926
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
9776
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
11095
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...
0
10716
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10812
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
9552
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...
0
5780
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...
1
4597
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
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.