Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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]

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You use those two constructs
    Code:
    if(document.form['shipform']......
    
    and
    
    document.forms['shipform']......
    The latter works ... you see the difference?

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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]

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 09-13-2011 at 10:40 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •