Web Analytics Made Easy -
StatCounter document.form is undefined - CodingForum

Announcement

Collapse
No announcement yet.

document.form is undefined

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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; Sep 13, 2011, 05:28 AM. Reason: wrap the code [code][/code]

  • #2
    You use those two constructs
    Code:
    if(document.form['shipform']......
    
    and
    
    document.forms['shipform']......
    The latter works ... you see the difference?

    Comment


    • #3
      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; Sep 13, 2011, 05:29 AM. Reason: wrap the code [code][/code]

      Comment


      • #4
        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([B]this.[COLOR="Blue"]form[/COLOR][/B])">
        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.[COLOR="Blue"]forms[/COLOR];
        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; Sep 13, 2011, 05:40 AM.
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          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).

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎