Click to See Complete Forum and Search --> : Accessing array value problem


mili
07-29-2003, 05:54 PM
I defined an array with a list of form names that I want to submit dynamically

var forms=new Array();
forms[0]="dummy";
forms[1]="Header";
forms[2]="Body";
forms[3]="Footer";
forms[4]="FileValidation";

I declared a variable and set it to 1
var previousIndex = 1;

when i alert the value of forms[previousIndex], I get the right form name like Header-for example
alert(forms[previousIndex]);

But when I use it in the DOM, it fails - forms[..] is null or not an object
self.content.document.forms[previousIndex].method="post";

Pls help me!

karayan
07-29-2003, 06:56 PM
I think you are confusing your own forms[] array with the forms[] object of the document object. In other words, the forms[] array you defined has nothing to do with the document.forms[] array, which is part of the DOM. The reason the latter shows up null is becuase the document has no forms in it. That is to say, there are no <form> tags in the body of your document.

I am not sure that you can actually SET the document.forms[] array values in Javascript. The "values" you try to set are really meant to be values for the document.forms[X].name property of the document.forms[] array. But since the doc has no <form> tags, document.forms[] does not exist.

If you let us know exactly what you are trying to do, maybe we can pull together a quick HTML file with the right <form> tags.

mili
07-29-2003, 07:22 PM
I agree what you say.I changed the array name to FormNames instead.
I confirmed again and I do have a <form> in my document.
It is wierd that when I alert my array, it works bhut does not work with the DOM

mili
07-29-2003, 07:34 PM
Here is my scenario.
I need to determine the action names and which form to submit dynamically.
I was able to create a similar array of actions and I could get it to work.
I have a tab navigation system.When the user moves from one tab to another, I want to submit the form from the tab he just left and fire an action to view the current tab.I could get the later part to work.How else can I dynamically submit the previous form?

karayan
07-29-2003, 07:34 PM
I'm not sure what you mean. The definition of the "forms" in the FormNames[] array has nothing to do with the DOM. The DOm takes its document.forms[] array from the <form> tags in the body. So, if I have:

<form name"blabla" .......>
....
</form>
<form name"bloobloo" .......>
....
</form>

then alert(document.form[0].name) for example will yield "blabla." The array names you define in the FormNames[] array, really need to be defined in the HTML portion. Aletrnatively, have unnamed forms in the HTML, like so:

<form .......>
....
</form>
<form .......>
....
</form>

and then address them as entries of the document.Forms[] array. You can give them names, etc., by using the properties of the document.forms[] array.

I know of no way to define the forms straight into the document.Forms[] array. You could create forms in the document dynamically (if tht's what you are trying to do} by using document.write() calls.

karayan
07-29-2003, 07:41 PM
OK, let me try some thoughts. Are all these tabs part of the same document? If going from tab to tab means going from document to document, it will not work. Each document has its own forms, and know nothing about forms of other docs. Do you load new docs as you go from tab to tab?

I will assume that all tabs are somehow in the same document. Then, the forms in each tab should be in the same document also and the forms[] array should work.

maybe you should give us a better idea of how the tabs are formed, and how the forms are enclosed in the tabs.

mili
07-29-2003, 08:07 PM
I'm sorry....I dont quite understand what you mean by having unnamed forms and then address them as entries of document.Forms[]
Below is my script and maybe will give you a better idea to spot where I'm going wrong..

leftOn = "/images/tab_left_on.gif";
rightOn = "/images/tab_right_on.gif";
leftOff = "/images/tab_left_off.gif";
rightOff = "/ppbweb/images/tab_right_off.gif";
colorOn = "#333366";
colorOff = "#5389BC";

var actions=new Array();
actions[0]="dummy";
actions[1]="EditHeaderTab";
actions[2]="EditBodyTab";
actions[3]="EditFooterTab";
actions[4]="EditFileValidationTab";


var Formss=new Array();
Formss[0]="dummy";
Formss[1]="Header";
Formss[2]="Body";
Formss[3]="Footer";
Formss[4]="FileValidation";

var previousIndex = 1;

function selectTab(tab, cnt, form) {

for (var j=1; j<=cnt; j++) {
if (j==tab) {

document.images["LTab"+j].src = leftOn;
document.getElementById("Tab"+j).bgColor = colorOn;
document.images["RTab"+j].src = rightOn;
} else {
document.images["LTab"+j].src = leftOff;
document.getElementById("Tab"+j).bgColor = colorOff;
document.images["RTab"+j].src = rightOff;


}
}

var prevIndex = previousIndex;

self.content.document.Formss["previousIndex"].method="post";
self.content.document.Formss["previousIndex"].target="content";

//alert(previousIndex);
//alert(Formss[previousIndex]);

//Hardcoding form name here as opposed to retreiving from the array...works well
self.content.document.Header.action.value = actions[previousIndex];
self.content.document.Header.nextaction.value = actions[tab];

previousIndex = tab;

self.content.document.Formss["previousIndex"].submit();


return true;

}

karayan
07-29-2003, 08:16 PM
Here is a crazy idea. Would this work?

self.content.document.Forms[Formss["previousIndex"]].submit();

mili
07-29-2003, 08:22 PM
good guess :)

bad luck though...I get the same error null or not an object

karayan
07-29-2003, 08:51 PM
I guess, what I don't understand is where these forms are instantiated. For example, where is the form named "Header" defined? In the HTML?

I'm also a bit confused with what the tab variable is.

Also, shouldn't those lines be:

self.content.document.Formss[previousIndex].submit();

and not:

self.content.document.Formss["previousIndex"].submit();


You have defined no entry in the Formss array indexed as "previousIndex".

Gotta go catch some ZZZZZ's right now. I'll check again the forum tomorrow sometime.

mili
07-29-2003, 10:17 PM
<QUOTE>I guess, what I don't understand is where these forms are instantiated. For example, where is the form named "Header" defined? In the HTML?
</QUOTE>

These forms are individual pages like header.html, body.html loaded in the iframe when the respective tabs are clicked.header.html is wrapped in a form called "Header", which is also defined in the array.

<QUOTE>
I'm also a bit confused with what the tab variable is.

Also, shouldn't those lines be:

self.content.document.Formss[previousIndex].submit();

and not:

self.content.document.Formss["previousIndex"].submit();
</QUOTE>

I'm sorry...I was just testing, I actually used the first line you mentioned


<QUOTE>
You have defined no entry in the Formss array indexed as "previousIndex".
</QUOTE>

I have a variable called var previousIndex = 1;
in my code

<QUOTE>
Gotta go catch some ZZZZZ's right now. I'll check again the forum tomorrow sometime.
</QUOTE>

Good Night :)

karayan
07-31-2003, 12:20 AM
I was talking about a string index of an array, versus a numerical index.

For example,

var myArray = new Array();

I can define numerical indeces for the array, such as myArray[0], myArray[5], and I can define string indeces for it, such as myArray["colors"] and myArray["shapes"]. The expression myArray["colors"] does NOT mean "find the array entry with the index being the value of variable colors. It means, find the array entry for the index equal to the string "colors".

Therefore, writing

previousIndex=1;

then Formss[previousIndex] and Formss["previousIndex"] are not the same thing. The former really means Formss[1] (1 being the value of previousIndex). The latter means the entry of the array Formss indexed by the string "previousIndex" which has nothing to do with the value of variable previousIndex. Obviously, Formss[previousIndex]=Formss[1], which is the Header form. On the other hand, Formss["previousIndex"] does not exist, hence the null object error.