...

View Full Version : document.form is undefined



captsaetar
09-13-2011, 08:16 AM
Getting a document.form is undefined when calling this select onchange event



function submitShipMethodChange(form,field){
if(document.form['shipform'].elements['shipping_method']){
var fldIndex = form.elements[field].selectedIndex;
var selectedMethod = form.elements[field].options[fldIndex].value;
document.forms['shipform'].elements['shipping_method'].value = selectedMethod;
document.forms['shipform'].submit();
}
}

<select name="shipping_method" onChange="submitShipMethodChange(this.shipform,'shipping_method');">

devnull69
09-13-2011, 08:43 AM
You use those two constructs


if(document.form['shipform']......

and

document.forms['shipform']......

The latter works ... you see the difference?

captsaetar
09-13-2011, 09:20 AM
Yes I see what you mean. I changed the form. to forms., but now a new error document.forms.shipform is undefined instead of just document.form is undefined



function submitShipMethodChange(form,field){
if(document.forms['shipform'].elements['shipping_method']){
var fldIndex = forms.elements[field].selectedIndex;
var selectedMethod = forms.elements[field].options[fldIndex].value;
document.forms['shipform'].elements['shipping_method'].value = selectedMethod;
document.forms['shipform'].submit();
}
}

<select name="shipping_method" onChange="submitShipMethodChange(this.shipform,'shipping_method');">

Kor
09-13-2011, 10:33 AM
1. form - this is a javaScript native reference of a particular form, but it is used only in the context of a child (the element) -> parent (the form) context. Like:



// pseudo-code
<element onsomeevent="someFunction(this.form)">

where this refers the element.

2. forms - this is a generic reference of all the forms which might exist on a document. This is the reason for it is used only as a property of the document object



var allForms=document.forms;


Now, do you sense the difference? You may refer a form from both ends: either from a child element of that form (up from the branch), or from the parent of all the forms: the document (down to the branch). In the later you need also either the name of the form, or its position in the DOM tree.

devnull69
09-13-2011, 12:29 PM
So basically you had to replace document.form[] with document.forms[] but not "form" with "forms"!

Second: You called your function with this.shipform as a parameter. I think it should have been this.form instead (as Kor pointed out).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum