PDA

View Full Version : Assignment of form input values to an array in Netscape 7.0


dmurray
10-06-2002, 06:49 AM
I have a form within a table that is assigned to a JavaScript variable. This variable is used to fill a <div></div> upon a mouseover event. Because there are other mouseover events on the page that use the same space, I need to restore the values to the form in the event of an accidental overwrite of the form.

The values for the text elements of the form are saved (storeForm() below) to an arrray following an onchange event from any of the text elements (maybe not very efficient but I was confused enough already). The initial and subsequent mouseover events that trigger the form use this arrray (updateForm() below).

Everything works fine in IE6. However, NS 7.0 and Mozilla 1.01 both fail to update the array. As indicated in the comments, I am able to see ...elements [i].attributes['value'].name but not the value when I step through storeForm().

I have attached the code I am working on just in case it may help. I would very much appreciate any push in the right direction.

<script language "JavaScript" type="text/javascript">

// Initialize array if it doesn't already exist
if (!document.forms[0]) {// Tested and ok
var formValue = new Array(18);
for (i=0; i<formValue.length; i++) {
formValue[i] = "1" // Using 1 as a marker
}}

// onchange event, store input text element values in array
// Works as expected on IE6
// Mozilla 1.01 and Netscape 7.0 fail to update the formValue array
// formValue.length is OK
// The function will return the values for all element attributes except ...attribute['value'].value
// ...attribute['value'].name works OK

function storeForm() {
for (i=0; i<formValue.length; i++) {
if (document.forms[0].elements[i].attributes['type'].value == 'text'){
formValue[i] = document.forms[0].elements [i].attributes['value'].value; // Assign current text value to array element
// I think this is where it fails
}}}

// Assign values to the input text boxes from the formValue array
// Works in IE6. The formValue array is not updated so it's not known if this workds in NS7.0 or Mozilla 1.01
// The default value from section.clientReg is displayed before the field is updated by the formValue array

function updateForm(){
thisform = document.forms[0];
for (i=0; i<thisform.length; i++) {
if (thisform.elements[i].attributes['type'].value == 'text'){
thisform.elements[i].setAttribute('value',formValue[i]);
</script>

ahosang
10-06-2002, 09:34 PM
Try:

if (document.forms[0].elements[i].type == 'text'){
formValue[i] = document.forms[0].elements [i].value;

dmurray
10-07-2002, 12:13 AM
ahosang

Many thanks for the quick response to my plea. Your suggestion work like a charm and has saved me many hours of frustration. I'll have to think about the why's. Do you have an online tutorial you could recommend? My book and the online JavaScript tutorial I use weren't much help.

Regards

Denny

ahosang
10-07-2002, 01:12 AM
You tries to use XML type DOM properties when simpler javascript(DOM 0) sufficed.

value is not an official property of an attribue node, but IE obviously accepts it. Mozilla is much stricter to the specs.
nodeValue would have worked instead.
Try www.w3c.org(and look for DOM in the menu) for DOM/XML specs and Javascript Bible(danny goodman) for a book on core cross-browser javascript

dmurray
10-07-2002, 01:31 AM
Again, many thanks. I suspect it will be a while before I can return the favor.