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

Uncaught exception when calling rowIndex.

When I call addField(), my table appears to be populated correctly
with a new row in the table with all the required fields. However,
when I call delete row on any new rows that have been created, I get
the following error:

Error: uncaught exception: [Exception... "Component returned failure
code: 0x80004002 (NS_NOINTERFACE)
[nsIDOMHTMLTableRowElement.rowIndex]" nsresult: "0x80004002
(NS_NOINTERFACE)" location: "JS frame ::
http://localhost:3000/form/edit_fields/1 :: removeField :: line 65"
data: no]

The error appears to happen when the last line of removeField() gets
called...specifically rowToDelete.rowIndex throws the exception.

I should mention that I am running this code on Firefox 1.0, however,
when I run it on IE6, it doesn't work either, but I don't know what
the error is (I just don't know how to get a trace of the JavaScript
errors).

thanks for your help!
Aaron.

<script type="text/javascript">
var newArray = new Array();
var exArray = new Array();

var delArr = new Array();

var tbCount=<%= @form.inputs.count %>;

function addField() {
newArray[tbCount]=document.createElement('INPUT');
table = document.getElementById('fieldTable');

row = document.createElement('tr');
row.id = 'n_' + (tbCount + 1);
fieldCell = document.createElement('td');
fieldCell.appendChild(document.createTextNode('Fie ld ' +
(tbCount + 1) + ':'));

nameCell = document.createElement('td');
nameInput = document.createElement('input');
nameInput.id = 'nname' + (tbCount + 1);
nameInput.name = 'nname[' + (tbCount + 1) + ']';
nameInput.type = 'text';
nameInput.size = '30';
nameCell.appendChild(nameInput);

descCell = document.createElement('td');
descInput = document.createElement('input');
descInput.id = 'newdesc' + (tbCount + 1);
descInput.name = 'newdesc[' + (tbCount + 1) + ']';
descInput.type = 'text';
descInput.size = '30';
descCell.appendChild(descInput);

delCell = document.createElement('td');
delLink = document.createElement('a');
delLink.href = "#";
delLink.setAttribute('onClick', "removeField(n_" + (tbCount+1) +
"); return false");
delLink.appendChild(document.createTextNode('Delet e'));
delCell.appendChild(delLink);

table.appendChild(row);
row.appendChild(fieldCell);
row.appendChild(nameCell);
row.appendChild(descCell);
row.appendChild(delCell);
tbCount++;
}

function removeField(rowToDelete) {
array = rowToDelete.id.split('_');
rowType = array[0];
rowId = array[1];

switch (rowType) {
case 'e':
break;
case 'n':
break;
}
table = document.getElementById('fieldTable');
table.deleteRow(rowToDelete.rowIndex);
}
</script>

Jul 23 '05 #1
7 2450


me*********@hotmail.com wrote:

when I call delete row on any new rows that have been created, I get
the following error:

Error: uncaught exception: [Exception... "Component returned failure
code: 0x80004002 (NS_NOINTERFACE)
[nsIDOMHTMLTableRowElement.rowIndex]" nsresult: "0x80004002
(NS_NOINTERFACE)" location: "JS frame ::
http://localhost:3000/form/edit_fields/1 :: removeField :: line 65"
data: no]

The error appears to happen when the last line of removeField() gets
called...specifically rowToDelete.rowIndex throws the exception. function removeField(rowToDelete) {
array = rowToDelete.id.split('_');
rowType = array[0];
rowId = array[1];

switch (rowType) {
case 'e':
break;
case 'n':
break;
}
table = document.getElementById('fieldTable');
table.deleteRow(rowToDelete.rowIndex);
}


We need to see where you call the function, exactly what you pass in as
an argument, make sure it is really a row element object.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
me*********@hotmail.com wrote:
[W]hen I call delete row on any new rows that have been created, I
get the following error:
Could you create a minimal example and provide a URL? It's much easier
to diagnose a problem when it can be seen in action.

[snip]
function addField() {
newArray[tbCount]=document.createElement('INPUT');
table = document.getElementById('fieldTable');

row = document.createElement('tr');
row.id = 'n_' + (tbCount + 1);
fieldCell = document.createElement('td');
fieldCell.appendChild(document.createTextNode('Fie ld '
+ (tbCount + 1) + ':'));
I would seriously consider learning to apply the var keyword within
functions. It is not a good idea to dump loads of variables into the
global object.

[snip]
delLink.setAttribute('onClick', "removeField(n_" + (tbCount+1)
+ "); return false");


Isn't the argument to removeField supposed to be a string? You should
change that second argument to

'removeField("n_' + (tbCount + 1) + '"); return false;'

That said, this isn't a reliable way to create event listeners (though
it will certainly work in Mozilla). If you plan on using this code in
a wider environment, take the traditional approach:

delLink.onclick = function() {
removeField('n_' + (tbCount + 1));
return false;
};

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
I don't have a webserver in which I can deploy my code, but I can give
you the full page source if that will help.

When I add a row to a table through JavaScript, is the internal
representation of the table and rows still accessable as if the rows
where there when the page was rendered initially? I noticed that when I
add a row using table.insertRow(table.rows.length) and then
alert(table.rows.length), the number is always the same. So, if I had 3
rows rendered initially, the alert would display three. When I append a
couple more rows using the code below, the row count would still be
three. That leads me to believe that A) The rows are appended in some
other structure, or B) I have no clue what I am doing! I am willing to
accept B over A at this point. =-)

As for the parameter to the move method being wrapped in quotes. If I
do that, it actually returns a string, but when I leave the quotes out,
it returns the HTMLTableRowElement, which works nice because I should
be able to get the rowIndex from it (but I am not, I am getting this
exception!).

Anyway, thanks for your help and quick replies! Below is the full
source for the page.
Thanks again!
AR.

<html>
<head>
<title></title>

<script type="text/javascript">
var newArray = new Array();
var exArray = new Array();
var delArr = new Array();

var tbCount=<%= @form.inputs.count %>;

function addField() {
table = document.getElementById('fieldTable');

row = table.insertRow(table.rows.length);
row.id = 'n_' + (tbCount + 1);
fieldCell = document.createElement('td');
fieldCell.appendChild(document.createTextNode('Fie ld ' + (tbCount +
1) + ':'));

nameCell = document.createElement('td');
nameInput = document.createElement('input');
nameInput.id = 'nname' + (tbCount + 1);
nameInput.name = 'nname[' + (tbCount + 1) + ']';
nameInput.type = 'text';
nameInput.size = '30';
nameCell.appendChild(nameInput);

descCell = document.createElement('td');
descInput = document.createElement('input');
descInput.id = 'newdesc' + (tbCount + 1);
descInput.name = 'newdesc[' + (tbCount + 1) + ']';
descInput.type = 'text';
descInput.size = '30';
descCell.appendChild(descInput);

delCell = document.createElement('td');
delLink = document.createElement('a');
delLink.href = "#";
delLink.setAttribute('onClick', "removeField(n_" + (tbCount+1) + ");
return false");
delLink.appendChild(document.createTextNode('Delet e'));
delCell.appendChild(delLink);

table.appendChild(row);
row.appendChild(fieldCell);
row.appendChild(nameCell);
row.appendChild(descCell);
row.appendChild(delCell);
tbCount++;
}

function removeField(rowToDelete) {
array = rowToDelete.id.split('_');
rowType = array[0];
rowId = array[1];

switch (rowType) {
case 'e':
break;
case 'n':
break;
}
alert(rowToDelete.rowIndex);
table = document.getElementById('fieldTable');
table.deleteRow(rowToDelete.rowIndex);
}
</script>

</head>
<body>
<%= start_form_tag( {:action => "save_fields", :id => @form.id}, "id"
=> "fieldForm") %>

<table id="fieldTable">
<% i = 0
@form.inputs.each do |field|
i = i + 1
%>
<tr id="e_<%= i %>">
<td>
Field <%= i %>:
</td>
<td>
<%= text_field "name", field.id, "value" => field.name %>
</td>
<td>
<%= text_field "description", field.id, "value" => field.description
%>
</td>
<td>
<a href="#" onClick="removeField(<%= 'e_' + i.to_s %>); return
false;">Delete</a>
<!-- %= link_to("Delete", :action => "delete_field", :id => field.id
) % -->
</td>
</tr>
<% end %>
</table>
<input type="submit" value="Save"/>
<%= hidden_field "field_count", "count", "value" => @form.inputs.count
%>
<%= end_form_tag %>
<a href="#" onClick="addField(); return false;">Add Field</a>

</body>
</html>

Jul 23 '05 #4
me*********@hotmail.com wrote:
I don't have a webserver in which I can deploy my code, but I can
give you the full page source if that will help.
In it's current form, no. Please show the *output* of the server-side
code (preferably with only a minimal set of input data).
I noticed that when I add a row using
table.insertRow(table.rows.length)
You can use

table.insertRow(-1);

to append a row.
and then alert(table.rows.length), the number is always the same.
In which browser? Both IE6 and Firefox 1.0 update the row count. I'd
expect Opera to as well.
As for the parameter to the move method being wrapped in quotes. If
I do that, it actually returns a string, but when I leave the
quotes out, it returns the HTMLTableRowElement [...]


My apologies. I should have double-checked. However, you shouldn't
rely on that behaviour. Obtaining a reference to an element through
its id attribute value alone is not well supported (and in my opinion,
a horrible feature to implement).

On the subject of sending a reference though, this can easily be
achieved with a slight modification to the code I posted:

function addField() {
/* ... */

var row = table.insertRow(-1);

/* ... */

delLink.onclick = function() {
removeField(row);
return false;
};

/* ... */
}

For that to work the variable, row, *must* be local (as above).

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
Thanks Michael, you have given me some things to think about. Listed
below is the output of the rendered HTML page.

Thanks again!
AR.

<html>
<head>
<title></title>

<script type="text/javascript">
var newArray = new Array();
var exArray = new Array();
var delArr = new Array();

var tbCount=1;

function addField() {
table = document.getElementById('fieldTable');

row = table.insertRow(table.rows.length);
row.id = 'n_' + (tbCount + 1);
fieldCell = document.createElement('td');
fieldCell.appendChild(document.createTextNode('Fie ld ' + (tbCount +
1) + ':'));

nameCell = document.createElement('td');
nameInput = document.createElement('input');
nameInput.id = 'nname' + (tbCount + 1);
nameInput.name = 'nname[' + (tbCount + 1) + ']';
nameInput.type = 'text';
nameInput.size = '30';
nameCell.appendChild(nameInput);

descCell = document.createElement('td');
descInput = document.createElement('input');
descInput.id = 'newdesc' + (tbCount + 1);
descInput.name = 'newdesc[' + (tbCount + 1) + ']';
descInput.type = 'text';
descInput.size = '30';
descCell.appendChild(descInput);

delCell = document.createElement('td');
delLink = document.createElement('a');
delLink.href = "#";
delLink.setAttribute('onClick', "removeField(n_" + (tbCount+1) + ");
return false");
delLink.appendChild(document.createTextNode('Delet e'));
delCell.appendChild(delLink);

table.appendChild(row);
row.appendChild(fieldCell);
row.appendChild(nameCell);
row.appendChild(descCell);
row.appendChild(delCell);
tbCount++;
}

function removeField(rowToDelete) {
array = rowToDelete.id.split('_');
rowType = array[0];
rowId = array[1];

swit:h (rowType) {
case 'e':
break;
case 'n':
break;
}
alert(rowToDelete.rowIndex);
table = document.getElementById('fieldTable');
table.deleteRow(rowToDelete.rowIndex);
}
</script>

</head>
<body>
<form action="/form/save_fields/1" id="fieldForm" method="post">

<table id="fieldTable">

<tr id="e_1">

<td>
Field 1:
</td>
<td>
<input id="name_1" name="name[1]" size="30" type="text" value="ID"
/>
</td>
<td>
<input id="description_1" name="description[1]" size="30"
type="text" value="Serial Number" />
</td>

<td>
<a href="#" onClick="removeField(e_1); return false;">Delete</a>
<!-- %= link_to("Delete", :action => "delete_field", :id => field.id
) % -->
</td>
</tr>

</table>
<input type="submit" value="Save"/>
<input id="field_count_count" name="field_count[count]" type="hidden"
value="1" />
</form>
<a href="#" onClick="addField(); return false;">Add Field</a>

</body>
</html>

Jul 23 '05 #6
OK, I figured out why the table.rows.length wasn't updating. If you
look at my code, I first do a

row = table.insertRow(XXXX);

then later in the code, I manually add that row to the table
again....some funky stuff must have been happening behind the scenes.

AR.

Jul 23 '05 #7


me*********@hotmail.com wrote:
function addField() {
table = document.getElementById('fieldTable');
It was already suggested to make sure your function uses local variables
e.g.
var table = ... row = table.insertRow(table.rows.length); var row = ...
and so on. That also means you can use
delLink.setAttribute('onClick', "removeField(n_" + (tbCount+1) + ");
return false");
delLink.onclick = function (evt) {
removeField(row);
return false;
};
here instead of what you have, setAttribute is unfortunately implemented
in different ways in different browsers.

table.appendChild(row);
Don't do that if you have called insertRow above to create the row.
Indeed even if you used createElement to create a row don't call
appendChild on the table itself, instead make sure you insert into a tbody.

function removeField(rowToDelete) { table = document.getElementById('fieldTable');
table.deleteRow(rowToDelete.rowIndex);
You could simply do
rowToDelete.parentNode.removeChild(rowToDelete);
for those two lines.

<table id="fieldTable">

<tr id="e_1">

<td>
Field 1:
</td>
<td>
<input id="name_1" name="name[1]" size="30" type="text" value="ID"
/>
</td>
<td>
<input id="description_1" name="description[1]" size="30"
type="text" value="Serial Number" />
</td>

<td>
<a href="#" onClick="removeField(e_1);


Don't rely on elements being accessible by variables of the name of the
id attribute, that works in IE and some other browsers (unfortunately
even in Firefox 1.0 in quirks mode) but for cross-browser support you
should use document.getElementById e.g.
<a onclick="var row;
if (document.getElementById && (row =
document.getElementById('e_1'))) {
removeField(row);
}
return false;"

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #8

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

Similar topics

6
by: Matt Bostock | last post by:
Hi, When I validate this page: http://www.drfunkenstein.net/using http://jigsaw.w3.org/css-validator/, I get the following results: ...
4
by: nrhayyal | last post by:
hi all, i am facing a problem in catching an exception which is uncaught in any of the catch block. not doing so will gives me coredump. I tried by rewriting set_unexpected() and set_terminate()...
8
by: scorpion53061 | last post by:
Additional information: Exception from HRESULT: 0x800A03EC. This code produced this error. I am pretty sure I got this to run a while back and I was wondering if you all see anything I am doing...
3
by: c.prerovsky | last post by:
Hi there, I started messing around with JavaScript OOP a few days ago and still can't get this one to work. There are many things wich keep confusing me, eg. the various ways to define a class...
2
by: funktacular | last post by:
Hi - I have some javascript that works when I run it from a server, but I need to run it locally. When I try to execute it locally I get the following error: Error: uncaught exception: Permission...
9
by: xhunter | last post by:
Hi, I have written my code to load some content through ajax, which works perfectly, then I thought of adding a timeout to retry the action in case it has failed or something. here is the code...
3
by: George2 | last post by:
Hello everyone, Just want to check whether my understanding is correct, Both (1) and (2) only covers Windows C++ platform. 1. If there is uncaught exception, destructor is not ensured to...
2
by: josephx | last post by:
Hello, I got some of these errors listed below after executing an HTTP Post MIDlet on CLDC/MIDP platform, "Nokia S40 DP 2.0 SDK 1.1" and "S40 5th Edition SDK Feature Pack 1" and even for S60's...
5
by: Paul Rubin | last post by:
I think I've asked about this before, but is there a way to set up Python to handle uncaught exceptions with pdb? I know about setting sys.except_hook to something that calls pdb, but this is...
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
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...
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...
0
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...

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.