473,378 Members | 1,564 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,378 software developers and data experts.

InsertRow() and rowIndex

Hi

I'm currently developing a form with a dynamic table. initially the table is
populated with data and at the end of each row there are two buttons:
addLevel and addItem. Each button creates a new row below the row the button
is in. The new row also has these buttons, but when pressing one of these
buttons, nothing happens.
Can someone tell me why?

here's the code to the addLevel button:

<script>
doc = document;
cbsTotal =0;
cbsLevel =0;
function addLevelTo(id,index) {
var tbl = doc.getElementById(id);
var cbsHidden = doc.getElementById("cbsTotal");
var rowID = (showRowCell (index)) + 1;
cbsLevel++;
if(id == "cbsBody"){
var newrow = tbl.insertRow(rowID);
alert("check: " + id);
var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "red";
var newdata ="<div align='left'><input name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'></div>";
newcol.innerHTML = newdata;

var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "green";
var newdata = "<div align='left'><input name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'></div>";
newcol.innerHTML = newdata;

var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<div align='left'><input name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'></div>";
newcol.innerHTML = newdata;

var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<input type='button' name='Add Level' value='Add Level'
onClick='addLevelTo('cbsBody',this);'> <input type='button' name='Add Item'
value='Add Item' onClick='addRowTo('cbsBody');'>";
newcol.innerHTML = newdata;

cbsTotal++;
cbsHidden.value = cbsTotal;
}else{
//do nothing
}
updateRowCounters(tbl);
}
</script>
<script>
//housekeeping functions
function updateRowCounters(form){
var sel1 =form.insertIndex
var sel2 =form.deleteIndex
sel1.options.length =0
sel2.options.length =0
for (var i =0;i <theTableBody.rows.length;i++){
sel1.options [i ] ==new Option(i,i)
sel2.options [i ] ==new Option(i,i)
}
form.removeRowBtn.disabled =(i==0)
}
</script>
<script>
function showRowCell (element) {
var cell, row;
if (element.parentNode) {
do
cell = element.parentNode;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentNode;
}
else if (element.parentElement) {
do
cell= element.parentElement;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentElement;
}
return row.rowIndex;
}
</script>
Jul 20 '05 #1
3 17919
DU
Marco Alting wrote:
Hi

I'm currently developing a form with a dynamic table. initially the table is
populated with data and at the end of each row there are two buttons:
addLevel and addItem. Each button creates a new row below the row the button
is in. The new row also has these buttons, but when pressing one of these
buttons, nothing happens.
Can someone tell me why?

Please refer to the thread "InsertCell problems" (august 14th) in this
newsgroup. I'll upload the page I did on this.
Your code is long and would imply a lot of time to examine.
Nevertheless, here are some comments on your code.
here's the code to the addLevel button:

<script>
If you're going to use DOM 2 methods, then best is to make your whole
code comply with W3C markup specs and to validate your markup code. This
is important as it eliminates rendering problems and potential sources
of bugs of all kinds.

<script type="text/javascript">
doc = document;
No need for such variable really.
cbsTotal =0;
cbsLevel =0;
function addLevelTo(id,index) {
var tbl = doc.getElementById(id);
var cbsHidden = doc.getElementById("cbsTotal");
Avoid source of confusion and error: you have a global variable with the
identifier cbsTotal and an id attribute value "cbsTotal". Just by
following better coding practices, you improve readability, successful
debugging with debugger softwares, review by others not familiar with
your code.
var rowID = (showRowCell (index)) + 1;
cbsLevel++;
if(id == "cbsBody"){
var newrow = tbl.insertRow(rowID);
alert("check: " + id);
var newcol = newrow.insertCell();
You need to give the index of the cell as a parameter, say like:

var newcol = newrow.insertCell(0);
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016

newcol.height = 25;
newcol.bgcolor = "red";
var newdata ="<div align='left'><input name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'></div>";
I recommend you either use innerHTML everywhere or just use W3C DOM 2
methods everywhere. Don't mix both methods. I personally believe that
DOM 2 methods work better and are more supported for this kind of code.

I wonder why you need a div in that cell. Why the extra node here?
You don't need to defined the horizontal alignment of a div in a ltr
language document. It's already left-aligned in ltr [direction] documents.

When you construct a string, you need to escape occurences of / & and
others. So here, <\/div>
newcol.innerHTML = newdata;

var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "green";
var newdata = "<div align='left'><input name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'></div>";
2 elements can not have the same id attribute value. At least, you're
going to have problems, bugs when trying to access such element(s).

what should browsers do with a call like:
document.getElementById("cbs")
?
newcol.innerHTML = newdata;

var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<div align='left'><input name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'></div>";
newcol.innerHTML = newdata;

var newcol = newrow.insertCell();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<input type='button' name='Add Level' value='Add Level'
onClick='addLevelTo('cbsBody',this);'> <input type='button' name='Add Item'
value='Add Item' onClick='addRowTo('cbsBody');'>";
I'm not sure the event handlers will work. I don't know. But there are
known ways to register events to elements which will work in all
browsers (Opera 7.x, MSIE 6 for windows, NS 7.x and Mozilla-based
browsers) as long as you avoid resorting to constructed strings and
resorting to innerHTML method.
newcol.innerHTML = newdata;

cbsTotal++;
cbsHidden.value = cbsTotal;
}else{
//do nothing
}
updateRowCounters(tbl);
}
</script>
<script>
//housekeeping functions
function updateRowCounters(form){
var sel1 =form.insertIndex
var sel2 =form.deleteIndex
sel1.options.length =0
sel2.options.length =0
for (var i =0;i <theTableBody.rows.length;i++){
sel1.options [i ] ==new Option(i,i)
sel2.options [i ] ==new Option(i,i)
}
form.removeRowBtn.disabled =(i==0)
I'm not sure the above instruction will work.
}
</script>
<script>
function showRowCell (element) {
var cell, row;
if (element.parentNode) {
do
cell = element.parentNode;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentNode;
}
else if (element.parentElement) {
Only MSIE 4 does not support parentNode, so, here I don't see the need
for this else block.
do
cell= element.parentElement;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentElement;
}
return row.rowIndex;
}
</script>


Without seeing the whole code, markup and css code, it's impossible to
suggest more.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #2
DU
Marco Alting wrote:
Wow! That's a whole lot of advice, Thanks!

The only thing is, I put the code in there as a reference, I was still
testing with it. But it can be no excuse for some of the advice you gave
me, so I'll clean up the code as much as possible. The only thing I
wanted to know actually was; how do get the rowIndex of a Javascript
generated table row? But again, all other comments are more than
welcome, since I'm not the Javascript expert...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

<script>
function showRowCell (element) {
var cell, row;
if (element.parentNode) {
do
cell = element.parentNode;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentNode;
}
else if (element.parentElement) {
do
cell= element.parentElement;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentElement;
}
return row.rowIndex;
}
</script>

I see nothing wrong with the given function. Nevertheless,
- type="text/javascript" is missing
- I would remove the blank space between the function name and fnction
parameters everywhere in your code: the javascript interpreter does that
anyway... so why not do it to save time?
- I would use element.offsetParent rather because the offsetParent of a
<td> is per definition a <tr>. So if there is a considerable sub-tree of
nodes within a <td>, then the code will bubble up faster (1 iteration in
the do..while loop) within this containment hierarcy to the <td> this
way than by using parentNode.

I might have been able to see what was wrong with a full testpage
though, seeing the html code too.

Thanks for your nice comments: I never get enough of this :)

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #3
Here's a link to the testpage, maybe you can help me some more with this.
I've tried to implement some of your comments, but it is still not working.

http://www.alting-multimedia.nl/test/test_dyntable.html

Thanks!

"DU" <dr*******@hotREMOVEmail.com> schreef in bericht
news:bh**********@news.eusc.inter.net...
Marco Alting wrote:
Wow! That's a whole lot of advice, Thanks!

The only thing is, I put the code in there as a reference, I was still
testing with it. But it can be no excuse for some of the advice you gave
me, so I'll clean up the code as much as possible. The only thing I
wanted to know actually was; how do get the rowIndex of a Javascript
generated table row? But again, all other comments are more than
welcome, since I'm not the Javascript expert...

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

<script>
function showRowCell (element) {
var cell, row;
if (element.parentNode) {
do
cell = element.parentNode;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentNode;
}
else if (element.parentElement) {
do
cell= element.parentElement;
while (cell.tagName.toLowerCase() != 'td')
row = cell.parentElement;
}
return row.rowIndex;
}
</script>

I see nothing wrong with the given function. Nevertheless,
- type="text/javascript" is missing
- I would remove the blank space between the function name and fnction
parameters everywhere in your code: the javascript interpreter does that
anyway... so why not do it to save time?
- I would use element.offsetParent rather because the offsetParent of a
<td> is per definition a <tr>. So if there is a considerable sub-tree of
nodes within a <td>, then the code will bubble up faster (1 iteration in
the do..while loop) within this containment hierarcy to the <td> this
way than by using parentNode.

I might have been able to see what was wrong with a full testpage
though, seeing the html code too.

Thanks for your nice comments: I never get enough of this :)

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #4

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

Similar topics

4
by: webdev | last post by:
Folks, Can someone please tell me why this simple piece of code, irritatingly, fails to give the expected result? Regards Rae MacLeman <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0...
3
by: milkyway | last post by:
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...
1
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...
2
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...
8
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 =...
1
by: Christopher Berg | last post by:
hi. is there a way to add an id='...' tag to a table row when using the javascript insertRow() ? i wish to be able to manipulate this row later if i need to using that identifier (eg to toggle...
1
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...
1
by: MoProtein | last post by:
I created a row using insertRow: r = tbl.insertRow(); and I want to be able to add an onclick handle to that row. Does any one know how to do this? BTW I've tried
3
by: DL | last post by:
Here's the thing, I have this HTML Table, that has multile rows already, each has an ID, one of them at the bottom section has id of "SaveData", new TRs are dynamically created upon user...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.