Web Analytics Made Easy -
StatCounter Passing a form name to a function - CodingForum

Announcement

Collapse
No announcement yet.

Passing a form name to a function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing a form name to a function

    I can't seem to get this to work without hard coding the form name. I will be processing multiple forms on the page and I need to be able to change the amount field on a specific form on the page. Any other suggestions?


    <html>
    <head>
    <title>Small Robe Orders</title>

    <script LANGUAGE="JavaScript1.1">
    function ChangeAmount(theForm){

    document.theForm.amount.value="12.00";

    }


    </script>
    </head>
    <body>
    <table width="600" height="400" bgcolor="#F8bf24" cellspacing="2" cellpadding="2" bordercolor="#0000ff" border="2" >
    <th align="center"colspan="2">Kia's Kaps </th>
    <tr><!-- Row 1 -->
    <td align="center"><img src="../PinkPatriot.jpg" width="150" height="142" alt="" border="0">
    <form name="form1" target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">

    <input type="text" name="amount" value="0.00">
    <input type="hidden" name="cn" value="Comments or Questions?">
    <table>
    <tr>
    <td><input type="hidden" name="on0" value="Size">Size</td>

    <td><select name="os0" onChange=ChangeAmount(this.form)>

    <option value="Small">Small
    <option value="Medium">Medium
    <option value="Large ">Large
    <option value="X Large">X Large
    <option value="XX Large">XX Large</select>
    </td></tr></table>
    <input type="image" src="https://www.paypal.com/images/sc-but-03.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
    <input type="hidden" name="add" value="1">
    </form><br>
    </body>
    </html>


  • #2
    You can refer to it by number

    document.forms[index of form starts at 0]

    Comment


    • #3
      I tried this and it did not work

      document.Forms[0].amount.value="12.00";

      Comment


      • #4
        <script LANGUAGE="JavaScript1.1">
        function ChangeAmount(theForm){

        document.forms[theForm].amount.value="12.00";

        }


        </script>
        bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

        i am a loser geek, crazy with an evil streak,
        yes i do believe there is a violent thing inside of me.

        Comment


        • #5
          Can't seem to pass either form name or index from the function

          It did not work. I am sending it correctly from the OnChange event? parameters ok?

          Comment


          • #6
            sorry, i wasn't paying enough attention. try this:

            <script LANGUAGE="JavaScript1.1">
            function ChangeAmount(theForm){
            theForm.amount.value="12.00";
            }


            </script>
            bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

            i am a loser geek, crazy with an evil streak,
            yes i do believe there is a violent thing inside of me.

            Comment


            • #7
              Every form element has a .form property which refers to the form (object) it's contained in. You referenced it properly when sending it along as an argument:

              onChange="ChangeAmount(this.form)">

              this refers to the Select object.

              When it's received at the other end, it naturally refers to the form itself so, no additional reference is required:

              function ChangeAmount(theForm) {
              theForm.amount.value="12.00";
              }

              You only need to use the full path - [window.]document.form_name.element_name - when no object reference is passed to the function. You can probably see why the passed reference approach is more flexible - the form doesn't even need to be named.

              Thought you might like an explanation...

              Comment


              • #8
                Thanks for your help

                Thanks so much for the all of the help today

                Comment

                Working...
                X