Array has no properties error

P

pbd22

Hi.

I have a script bellow that, when it gets called,
throws "fileList[myFile] has no properties. I have
checked to make sure that all the elements on
the page that the loop calls are there and have
id/name tags and everything checks out. Not
sure what is causing this:


<script type="text/javascript">

var fileList = new Array();

function addFile(myFile) {

try{

var frm = document.uploadForm;

for (var f = 0; f < frm.elements.length; f++)

{
if (frm.elements[f].className == "input") {
fileList[myFile][frm.elements[f].name] = '';
}
}

}catch(e){alert(e.message + " and the count is " + f);}

} ...


IF the above code doesn't show you what my problem is, then
I have added some more information (SKIP it if you don't need it).
Otherwise, continue reading...

The addFile function is called inside addRow() which is here:

//adds a DIV row with file link in innerHTML
addRow:function(){

if( this.max == 0 || this.elements.length <= this.max ){

current_element = this.elements.getLast();

// Create new row in files list
var name = current_element.element.value;

addFile(name);

[SNIP] ...

And, addRow is initialized below (called from the init function):

initializeElement:function( element ){

// What happens when a file is selected
element.addEvent(
'change',
function(){
this.addRow()
}.bind( this )
);
 
T

Thomas 'PointedEars' Lahn

pbd22 said:
I have a script bellow that, when it gets called,
throws "fileList[myFile] has no properties.

BAD. Broken as designed.
I have checked to make sure that all the elements on
the page that the loop calls are there and have
id/name tags and everything checks out.

But you have not checked your user-defined objects.
Not sure what is causing this:

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

You have initialized the array ...
function addFile(myFile) {
try{
var frm = document.uploadForm;

Should be

document.forms["uploadForm"]

but if you pass `this.form' from an event handler attribute value
of a form control within a `form' element to addFile(), you have the
reference already (or pass `this' for an `o' argument and then do
`var frm = o.form;').
for (var f = 0; f < frm.elements.length; f++)

for (var f = frm.elements.length; f--;)

is more efficient and sufficient in your case. You could even increase
efficiency more by assigning a reference to `frm.elements' to a local
variable and then using this variable instead.

I'd rather use `i' instead of `f' here.
{
if (frm.elements[f].className == "input") {
fileList[myFile][frm.elements[f].name] = '';

.... but you have not initialized the Array object property with an object
reference. `fileList[myFile]' therefore remains `undefined', no matter the
value of `myFile'. And `undefined' neither has any properties nor can it be
augmented with them.

Add the following before the above line (provided that `myFile' is numeric,
otherwise you are better off with an Object object there, too; search the
archives):

fileList[myFile] = new Object();


HTH

PointedEars
 
P

pbd22

Thank you. The Object object bit fixed my "no properties" error.
As a follow up, if you don't mind, I have an additional question.
In the below method I am attempting to assign an onclick method
dynamically:

document.getElementById(frm.elements.id).onclick = 'editFile('+
myFile+',this.name,this.value)';

But, when I click on the element, I never get any reaction. I have
tried putting a simple
alert inside editFile and I have tried replacing the edieFile onclick
method with a simple alert.
Doesn't seem to have any affect. When I try to view selection source
in Firefox, it doesn't show
the onclick attribute.

Why isn't this working?

Thanks again.


function selectFile(myFile) {

try {

var frm = document.forms["uploadForm"];

if (fileList[myFile].length > 0) {
for(key in fileList[myFile]) {
document.getElementById(key).value =
fileList[myFile][key];
}
}

for (var i = 0; i < frm.elements.length; i--)
{
if (frm.elements.className == "input") {

document.getElementById(frm.elements.id).onclick = 'editFile('+
myFile+',this.name,this.value)';
}

}

}catch(e){alert("selectFile: " + e.message);}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top