473,837 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DOM table simple question



Why does the 'insertrow' work in this situation:

<html>
<script type="text/javascript">
function addRow(tableID) {
var tableRef = document.getEle mentById(tableI D);
var newRow = tableRef.insert Row(2);
var newCell = newRow.insertCe ll(0);
var newText = document.create TextNode('New second cell')
newCell.appendC hild(newText);
}
</script>
<table id="TableA">
<tr><td>firstce ll</td><tr>
<tr><td>secondc ell</td><tr>
<tr><td>thirdce ll</td><tr>
</table>
<input type="button" value="try me" onclick="addRow ('TableA');">
</html>
But not here (I didn't even bother to do the insertRow(2) here because
the domtable contains zero rows, I get 'index or size is negative or
greater than the nbr of rows') - just run the html below, please :

<html>
<head>
<script>
var arr=[];
arr[0]="first row";
arr[1]="second row";
arr[2]="third row";
arr[3]="fourth row";
function domtable() {
var myrow, mytd, mytext, mytable, attr;
mytable=documen t.createElement ("table");
mytable.id='mij ntabel';
for (i=0; i < arr.length ;i++){
myrow=document. createElement(" tr");
mytd=document.c reateElement("t d");
mytext=document .createTextNode (arr[i]);
mytd.appendChil d(mytext);
myrow.appendChi ld(mytd);
mytable.appendC hild(myrow);

}
document.body.a ppendChild(myta ble);
}
function howmanyrows() {
var eerstetabel = document.getEle mentsByTagName( "table");
alert('the browser says that this table has ' + eerstetabel
[0].rows.length + ' rows, which is not true');
}
</script>
</head>
<body>
<input type="button" value="run me first" onclick="domtab le();">
<input type="button" value="then check the number of rows"
onclick="howman yrows();">
</body>
</html>

It appears you can only "count" the number of rows in a real html table.
In my app I have a DOM table whose rows start with a plus sign. If you
click the plus, a new row should insert beneath it. Do I really have to
"id attribute" every single 'plus cell', I was hoping I could use the
index of my for loop.
I also read some about tbody, but why is tbody not necessary in the first
example?

Targetbrowser is FF 1.5

thx for any advise.

Ward
May 14 '06 #1
3 1482


King Albert wrote:

mytable=documen t.createElement ("table");
Create a tbody e.g.
var tbody = document.create Element("tbody" );
mytable.appendC hild(tbody);
then insert the rows into the tbody e.g.
mytable.appendC hild(myrow);


tbody.appendChi ld(myrow);

If the HTML parser parses the HTML table markup then it implicitly
creates a tbody child if it is not in the markup.

--

Martin Honnen
http://JavaScript.FAQTs.com/
May 14 '06 #2
Martin Honnen <ma*******@yaho o.de> wrote in news:44675a6f$0 $11078
$9*******@newsr ead4.arcor-online.net:


King Albert wrote:

mytable=documen t.createElement ("table");


Create a tbody e.g.
var tbody = document.create Element("tbody" );
mytable.appendC hild(tbody);
then insert the rows into the tbody e.g.
mytable.appendC hild(myrow);


tbody.appendChi ld(myrow);

If the HTML parser parses the HTML table markup then it implicitly
creates a tbody child if it is not in the markup.

thx for your advise,

The alert box below now acknowledges 4 rows.
Thx very much,

Ward

<html>
<head>
<script>
var arr=[];
arr[0]="first row";
arr[1]="second row";
arr[2]="third row";
arr[3]="fourth row";

function domtable() {
var myrow, mytd, mytext, mytable, attr;
mytable=documen t.createElement ("table");
mytable.id='mij ntabel';
mytbody=documen t.createElement ("tbody");

for (i=0; i < arr.length ;i++){
myrow=document. createElement(" tr");
mytd=document.c reateElement("t d");
mytext=document .createTextNode (arr[i]);
mytd.appendChil d(mytext);
myrow.appendChi ld(mytd);
mytbody.appendC hild(myrow);
}
mytable.appendC hild(mytbody);
document.body.a ppendChild(myta ble);
}
function howmanyrows() {
var eerstetabel = document.getEle mentsByTagName( "tbody");
alert('the browser says that this table has ' + eerstetabel
[0].rows.length + ' rows, which is true');
}
</script>
</head>
<body>

<input type="button" value="run me first" onclick="domtab le();">

<input type="button" value="then check the number of rows"
onclick="howman yrows();">

</body>

</html>

May 14 '06 #3
King Albert wrote:
Martin Honnen <ma*******@yaho o.de> wrote in news:44675a6f$0 $11078
$9*******@newsr ead4.arcor-online.net:

King Albert wrote:

mytable=documen t.createElement ("table"); Create a tbody e.g.
var tbody = document.create Element("tbody" );
mytable.appendC hild(tbody);
then insert the rows into the tbody e.g.
mytable.appendC hild(myrow);

tbody.appendChi ld(myrow);

If the HTML parser parses the HTML table markup then it implicitly
creates a tbody child if it is not in the markup.

thx for your advise,

The alert box below now acknowledges 4 rows.
Thx very much,


An alternative is to use insertRow(-1) which will insert the row as the
last row and you don't have to explicitly create the tbody. Use
insertCell(-1) for the cells.

<html>
<head>
<script>
var arr=[];
arr[0]="first row";
arr[1]="second row";
arr[2]="third row";
arr[3]="fourth row";

function domtable() {
var myrow, mytd, mytext, mytable, attr;
mytable=documen t.createElement ("table");
mytable.id='mij ntabel';
mytbody=documen t.createElement ("tbody");

for (i=0; i < arr.length ;i++){
myrow=document. createElement(" tr");
mytd=document.c reateElement("t d");
mytext=document .createTextNode (arr[i]);
mytd.appendChil d(mytext);
myrow.appendChi ld(mytd);
mytbody.appendC hild(myrow);
}
mytable.appendC hild(mytbody);

Or don't insert the tbody and:

for (var i=0; i < arr.length ;i++){
myrow = mytable.insertR ow(-1);
mytd = myrow.insertCel l(-1);
mytd.appendChil d(document.crea teTextNode(arr[i]));
}
document.body.a ppendChild(myta ble);
}


[...]

--
Rob
May 14 '06 #4

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

Similar topics

3
3613
by: Raaijmakers, Vincent (IndSys, GE Interlogix) | last post by:
Ok, perhaps a question on a newbie level. I try to create a simple 'write to a console application' where all the items in a string do have a variable size: items = Well, actually, I need to print a table as simple text, nice lined up in a console. So: Item: Value: Another Value:
1
2101
by: Eric | last post by:
Hello, I'm sure your all tired of these "alignment" question, but please endure mine. Question. Simple put: What makes a table sit beside another table, as oppose to going down below it. Here's my example ... <html>
15
2151
by: kimi | last post by:
I have just started working on a project that is partially complete. It is an application that is using access to store test results. The test results are being stored in two Access 2000 databases. DB #1 = StudentDB DB #2 = TestResulstsDB Why are there 2 dbs? I do not know - but that is one of the tings that we will be changing. Combining all of the data into one database.
1
2133
by: Greg | last post by:
Hi, I need to implement a table in XBRL. Let's assume I have 2 simple tables to define: TABLE 1 col1 col2 row1 A C row2 B D TABLE 2
3
4540
by: Odawg | last post by:
Hello All Database (Access) Guru's, I am a novice when it comes to databases and I know enough to get simple information for my needs. With that said, I was given an opportunity for improvement a database. heres the scenario or process that I am facing 1. A total of 3 text files are generated from the mainframe and save to a secure network share. In each text file only raw data
1
3646
by: LurfysMa | last post by:
I am working on an electronic flashcard program. Most of the subjects are simple lists of questions and answers. Those seem to be working. Some of the "subjects" have "categories" of questions. One example is a kind of trivia subject. There are several "categories" such as "geography", "history", etc., like in the Trivial Pursuit game. Each card has a question in each category.
9
1740
by: sck10 | last post by:
Hello, I am building a web form that will be used to gather information for marketing plans. The form will have 15 questions which must be answered. Each question can have large blocks of text. The issue is that the user will go to the web to enter and update their information over time, so all the questions need to be on one form. I believe the proper structure for the table would be: MarketingRequest
16
10333
by: pukivruki | last post by:
hi, I wish to create a temporary table who's name is dynamic based on the argument. ALTER PROCEDURE . @PID1 VARCHAR(50), @PID2 VARCHAR(50), @TICKET VARCHAR(20)
3
3063
by: jmDesktop | last post by:
Hi, probably a simple question that I have for what I think is a simple database. I have a Vendors table and an Items table. There is only one items, but several Vendors may have that same item. I have to have a third table, correct, to connect them? I was thinking OrderDetails (or something like that) OrderDetailID
2
10156
by: Eraser | last post by:
Hello, I'm just starting to learn PL/SQL. To get my feet wet, I'm trying to write a simple stored procedure that takes some values as parameters, and inserts those values into a table. For some reason my simple procedure is not working, I'm probably missing something simple. Here is how I'm trying to create this procedure: CREATE OR REPLACE PROCEDURE insert_person(uid IN NUMBER,
0
9846
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
10580
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...
0
10277
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9413
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
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5674
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
5853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
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.