Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-13-2011, 08:16 AM   PM User | #1
captsaetar
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
captsaetar is an unknown quantity at this point
document.form is undefined

Getting a document.form is undefined when calling this select onchange event

Code:
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');">

Last edited by Kor; 09-13-2011 at 10:28 AM.. Reason: wrap the code [code][/code]
captsaetar is offline   Reply With Quote
Old 09-13-2011, 08:43 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You use those two constructs
Code:
if(document.form['shipform']......

and

document.forms['shipform']......
The latter works ... you see the difference?
devnull69 is offline   Reply With Quote
Old 09-13-2011, 09:20 AM   PM User | #3
captsaetar
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
captsaetar is an unknown quantity at this point
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

Code:
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');">

Last edited by Kor; 09-13-2011 at 10:29 AM.. Reason: wrap the code [code][/code]
captsaetar is offline   Reply With Quote
Old 09-13-2011, 10:33 AM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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:

Code:
// 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

Code:
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.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 09-13-2011 at 10:40 AM..
Kor is offline   Reply With Quote
Old 09-13-2011, 12:29 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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).
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:11 PM.


Advertisement
Log in to turn off these ads.