473,564 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,i ndex) {
var tbl = doc.getElementB yId(id);
var cbsHidden = doc.getElementB yId("cbsTotal") ;
var rowID = (showRowCell (index)) + 1;
cbsLevel++;
if(id == "cbsBody"){
var newrow = tbl.insertRow(r owID);
alert("check: " + id);
var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "red";
var newdata ="<div align='left'><i nput name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'> </div>";
newcol.innerHTM L = newdata;

var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "green";
var newdata = "<div align='left'><i nput name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'> </div>";
newcol.innerHTM L = newdata;

var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<div align='left'><i nput name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'> </div>";
newcol.innerHTM L = newdata;

var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<input type='button' name='Add Level' value='Add Level'
onClick='addLev elTo('cbsBody', this);'> <input type='button' name='Add Item'
value='Add Item' onClick='addRow To('cbsBody');' >";
newcol.innerHTM L = newdata;

cbsTotal++;
cbsHidden.value = cbsTotal;
}else{
//do nothing
}
updateRowCounte rs(tbl);
}
</script>
<script>
//housekeeping functions
function updateRowCounte rs(form){
var sel1 =form.insertInd ex
var sel2 =form.deleteInd ex
sel1.options.le ngth =0
sel2.options.le ngth =0
for (var i =0;i <theTableBody.r ows.length;i++) {
sel1.options [i ] ==new Option(i,i)
sel2.options [i ] ==new Option(i,i)
}
form.removeRowB tn.disabled =(i==0)
}
</script>
<script>
function showRowCell (element) {
var cell, row;
if (element.parent Node) {
do
cell = element.parentN ode;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentNode ;
}
else if (element.parent Element) {
do
cell= element.parentE lement;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentElem ent;
}
return row.rowIndex;
}
</script>
Jul 20 '05 #1
3 17929
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,i ndex) {
var tbl = doc.getElementB yId(id);
var cbsHidden = doc.getElementB yId("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(r owID);
alert("check: " + id);
var newcol = newrow.insertCe ll();
You need to give the index of the cell as a parameter, say like:

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

newcol.height = 25;
newcol.bgcolor = "red";
var newdata ="<div align='left'><i nput 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.innerHTM L = newdata;

var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "green";
var newdata = "<div align='left'><i nput 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.getEle mentById("cbs")
?
newcol.innerHTM L = newdata;

var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<div align='left'><i nput name='cbs" + cbsTotal + "
type='text' id='cbs" + cbsTotal + " size='20' maxlength='40'> </div>";
newcol.innerHTM L = newdata;

var newcol = newrow.insertCe ll();
newcol.height = 25;
newcol.bgcolor = "blue";
var newdata = "<input type='button' name='Add Level' value='Add Level'
onClick='addLev elTo('cbsBody', this);'> <input type='button' name='Add Item'
value='Add Item' onClick='addRow To('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.innerHTM L = newdata;

cbsTotal++;
cbsHidden.value = cbsTotal;
}else{
//do nothing
}
updateRowCounte rs(tbl);
}
</script>
<script>
//housekeeping functions
function updateRowCounte rs(form){
var sel1 =form.insertInd ex
var sel2 =form.deleteInd ex
sel1.options.le ngth =0
sel2.options.le ngth =0
for (var i =0;i <theTableBody.r ows.length;i++) {
sel1.options [i ] ==new Option(i,i)
sel2.options [i ] ==new Option(i,i)
}
form.removeRowB tn.disabled =(i==0)
I'm not sure the above instruction will work.
}
</script>
<script>
function showRowCell (element) {
var cell, row;
if (element.parent Node) {
do
cell = element.parentN ode;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentNode ;
}
else if (element.parent Element) {
Only MSIE 4 does not support parentNode, so, here I don't see the need
for this else block.
do
cell= element.parentE lement;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentElem ent;
}
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.parent Node) {
do
cell = element.parentN ode;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentNode ;
}
else if (element.parent Element) {
do
cell= element.parentE lement;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentElem ent;
}
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.offsetP arent 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*******@hotR EMOVEmail.com> schreef in bericht
news:bh******** **@news.eusc.in ter.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.parent Node) {
do
cell = element.parentN ode;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentNode ;
}
else if (element.parent Element) {
do
cell= element.parentE lement;
while (cell.tagName.t oLowerCase() != 'td')
row = cell.parentElem ent;
}
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.offsetP arent 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
9473
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 Transitional//EN"> <html>
3
16190
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 addRowDOM (tableID) {
1
3161
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,...
2
9518
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
3457
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);
1
2852
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 its display style). thank you.
1
15778
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)
1
2597
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
3213
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 interaction, for instance, var tbl = document.getElementById ('mytable'); // pick a TR position to insert a new row var trPos = 6 newTR =...
0
7665
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...
0
7583
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...
0
8106
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...
0
7950
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...
0
6255
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
0
924
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...

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.