473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DOM table simple question



Why does the 'insertrow' work in this situation:

<html>
<script type="text/javascript">
function addRow(tableID) {
var tableRef = document.getElementById(tableID);
var newRow = tableRef.insertRow(2);
var newCell = newRow.insertCell(0);
var newText = document.createTextNode('New second cell')
newCell.appendChild(newText);
}
</script>
<table id="TableA">
<tr><td>firstcell</td><tr>
<tr><td>secondcell</td><tr>
<tr><td>thirdcell</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=document.createElement("table");
mytable.id='mijntabel';
for (i=0; i < arr.length ;i++){
myrow=document.createElement("tr");
mytd=document.createElement("td");
mytext=document.createTextNode(arr[i]);
mytd.appendChild(mytext);
myrow.appendChild(mytd);
mytable.appendChild(myrow);

}
document.body.appendChild(mytable);
}
function howmanyrows() {
var eerstetabel = document.getElementsByTagName("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="domtable();">
<input type="button" value="then check the number of rows"
onclick="howmanyrows();">
</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 1463


King Albert wrote:

mytable=document.createElement("table");
Create a tbody e.g.
var tbody = document.createElement("tbody");
mytable.appendChild(tbody);
then insert the rows into the tbody e.g.
mytable.appendChild(myrow);


tbody.appendChild(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*******@yahoo.de> wrote in news:44675a6f$0$11078
$9*******@newsread4.arcor-online.net:


King Albert wrote:

mytable=document.createElement("table");


Create a tbody e.g.
var tbody = document.createElement("tbody");
mytable.appendChild(tbody);
then insert the rows into the tbody e.g.
mytable.appendChild(myrow);


tbody.appendChild(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=document.createElement("table");
mytable.id='mijntabel';
mytbody=document.createElement("tbody");

for (i=0; i < arr.length ;i++){
myrow=document.createElement("tr");
mytd=document.createElement("td");
mytext=document.createTextNode(arr[i]);
mytd.appendChild(mytext);
myrow.appendChild(mytd);
mytbody.appendChild(myrow);
}
mytable.appendChild(mytbody);
document.body.appendChild(mytable);
}
function howmanyrows() {
var eerstetabel = document.getElementsByTagName("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="domtable();">

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

</body>

</html>

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

King Albert wrote:

mytable=document.createElement("table"); Create a tbody e.g.
var tbody = document.createElement("tbody");
mytable.appendChild(tbody);
then insert the rows into the tbody e.g.
mytable.appendChild(myrow);

tbody.appendChild(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=document.createElement("table");
mytable.id='mijntabel';
mytbody=document.createElement("tbody");

for (i=0; i < arr.length ;i++){
myrow=document.createElement("tr");
mytd=document.createElement("td");
mytext=document.createTextNode(arr[i]);
mytd.appendChild(mytext);
myrow.appendChild(mytd);
mytbody.appendChild(myrow);
}
mytable.appendChild(mytbody);

Or don't insert the tbody and:

for (var i=0; i < arr.length ;i++){
myrow = mytable.insertRow(-1);
mytd = myrow.insertCell(-1);
mytd.appendChild(document.createTextNode(arr[i]));
}
document.body.appendChild(mytable);
}


[...]

--
Rob
May 14 '06 #4

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

Similar topics

3
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...
1
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. ...
15
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....
1
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
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...
1
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. ...
9
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....
16
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
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. ...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.