referencing javascript form object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phpcode

    #1

    referencing javascript form object

    I have a javascript function as follows:

    function myfunction(form Name,formField) {
    parseInt(docume nt.formName.for mField.value) +=1;
    }

    Then I can call the function when needed like this:

    myfunction(user s,username);

    Here is my problem:

    Javascript does not evaluate the arguments passed to the function.
    Instead, it tries to use the arguments literarilly.

    So instead of this:

    parseInt(docume nt.users.userna me.value) +=1;

    javascript is actually looking for this:

    parseInt(docume nt.formName.for mField.value) +=1;

    I have also tried the following without success:

    parseInt(eval(d ocument.formNam e.formField).va lue) +=1;

    Any suggestions on how to solve this problem?

  • Randy Webb

    #2
    Re: referencing javascript form object

    phpcode wrote:[color=blue]
    > I have a javascript function as follows:
    >
    > function myfunction(form Name,formField) {
    > parseInt(docume nt.formName.for mField.value) +=1;
    > }
    >
    > Then I can call the function when needed like this:
    >
    > myfunction(user s,username);
    >
    > Here is my problem:
    >
    > Javascript does not evaluate the arguments passed to the function.
    > Instead, it tries to use the arguments literarilly.
    >
    > So instead of this:
    >
    > parseInt(docume nt.users.userna me.value) +=1;
    >
    > javascript is actually looking for this:
    >
    > parseInt(docume nt.formName.for mField.value) +=1;
    >
    > I have also tried the following without success:
    >
    > parseInt(eval(d ocument.formNam e.formField).va lue) +=1;
    >
    > Any suggestions on how to solve this problem?
    >[/color]

    You start by reading the group FAQ.

    function myFunction(user s,username){
    parseInt(docume nt.forms[users].elements[username].value,10) += 1;
    }

    Note that I added the radix of 10 to the parseInt call. And whether
    parseInt is the best tool for the job depends on what the function is
    attempting to do.

    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • Lee

      #3
      Re: referencing javascript form object

      phpcode said:[color=blue]
      >
      >I have a javascript function as follows:
      >
      >function myfunction(form Name,formField) {
      >parseInt(docum ent.formName.fo rmField.value) +=1;
      >}
      >
      >Then I can call the function when needed like this:
      >
      >myfunction(use rs,username);
      >
      >Here is my problem:
      >
      >Javascript does not evaluate the arguments passed to the function.
      >Instead, it tries to use the arguments literarilly.
      >
      >So instead of this:
      >
      >parseInt(docum ent.users.usern ame.value) +=1;
      >
      >javascript is actually looking for this:
      >
      >parseInt(docum ent.formName.fo rmField.value) +=1;
      >
      >I have also tried the following without success:
      >
      >parseInt(eval( document.formNa me.formField).v alue) +=1;
      >
      >Any suggestions on how to solve this problem?[/color]

      Fix your function.
      It should take a reference to a field as an argument.
      Avoid using names when references are available.


      function myfunction(form FieldReference) {
      formFieldRefere nce.value++;
      }

      and call it as:

      myfunction(docu ment.formName.f ormField);

      or with some other reference to the field.

      Comment

      • phpcode

        #4
        Re: referencing javascript form object

        Randy,

        Thanks for your answer.

        However, you did not understand my question.

        The form fieldnames are dynamically generated. I do not know them, and
        cannot give the function the actual values. What I can do is pass it to
        the function while calling the function.

        I then want javascript to extract the real name from the arguments passed
        to it.

        In other words, I cannot do

        function myfunction(user s,username) {
        }

        because "users,username " changes each time the function is called.

        What I can do is pass the new values to javascript at runtime.

        Comment

        • Lee

          #5
          Re: referencing javascript form object

          phpcode said:[color=blue]
          >
          >Randy,
          >
          >Thanks for your answer.
          >
          >However, you did not understand my question.
          >
          >The form fieldnames are dynamically generated. I do not know them, and
          >cannot give the function the actual values. What I can do is pass it to
          >the function while calling the function.
          >
          >I then want javascript to extract the real name from the arguments passed
          >to it.
          >
          >In other words, I cannot do
          >
          >function myfunction(user s,username) {
          >}
          >
          >because "users,username " changes each time the function is called.
          >
          >What I can do is pass the new values to javascript at runtime.[/color]

          That's what his example shows.

          Comment

          • Randy Webb

            #6
            Re: referencing javascript form object

            phpcode wrote:[color=blue]
            > Randy,
            >
            > Thanks for your answer.
            >
            > However, you did not understand my question.[/color]

            OK.
            [color=blue]
            > The form fieldnames are dynamically generated. I do not know them, and
            > cannot give the function the actual values. What I can do is pass it to
            > the function while calling the function.[/color]

            And then you use the function I posted. It takes the dynamic names as
            parameters to the function.
            [color=blue]
            > I then want javascript to extract the real name from the arguments passed
            > to it.[/color]

            How are you calling the function? If users and username is the text that
            gets passed, like this:

            function myFunction(form Name,fieldName) {
            document.forms[formName].elements[fieldName].value++;
            }

            And call it like this:

            myFunction('som eFormName','som eUserName')

            Then the function will try to evaluate this:

            document.forms['someFormName'].elements['someUserName'].value;

            If that is not what you are trying to do, post some page code. Or, a URL.

            And please read the FAQ with regards to quoting.



            --
            Randy
            comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

            Comment

            • Robert

              #7
              Re: referencing javascript form object

              In article
              <ff64e82a3acc9b a32aee7eff45f5b f10@localhost.t alkaboutprogram ming.com>,
              "phpcode" <questions@oasi soflove.com> wrote:
              [color=blue]
              > I have a javascript function as follows:
              >
              > function myfunction(form Name,formField) {
              > parseInt(docume nt.formName.for mField.value) +=1;
              > }[/color]


              parseInt(docume nt.formName.for mField.value) +=1;
              Causes an error in Netscape 7.1. parseInt returns a value.

              In the following example, I show how a sting can be used to access a
              form element. I put in some alerts to show the flow of the program and
              how to debug problems.

              Robert

              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
              "http://www.w3.org/TR/html4/loose.dtd">
              <html>
              <head>
              <title>Simple validate</title>

              <script type="text/javascript">

              function myfunction(form Name,formField) {


              alert('formName = ' + formName +
              ' formField = ' + formField);

              alert( document.forms[formName] )
              alert( document.forms[formName].elements[formField] )
              var newValue =
              parseInt(docume nt.forms[formName].elements[formField].value,10) +
              1;
              alert ('newValue = ' + newValue)
              document.forms[formName].elements[formField].value = newValue;
              }

              </script>
              </head>
              <body>
              <p>
              The HTML file demonstrates how to validate a form field.</p>
              <p>The form is submitted to the host when it doesn't contain an asterisk
              (*).
              </p>
              <form name="myForm"
              action="http://www.natAValidWe bAddress.com"
              method="POST"
              onsubmit="alert ('onsubmit...') ;myfunction('my Form','theAddre ss');">

              <p>Address:&nbs p;
              <input type="text" name="theAddres s" id='theAddress' size="40"></p>
              <br>
              <input type="submit" value="Submit address information">
              </form>
              </body>
              </html>

              Comment

              • phpcode

                #8
                Re: referencing javascript form object

                I have tried all different combinations as suggested in this thread. Non
                of them works.

                Please try this demo page, and see the source:



                Comment

                • McKirahan

                  #9
                  Re: referencing javascript form object

                  "phpcode" <questions@oasi soflove.com> wrote in message
                  news:c6d0e6dbee 9235b446dfb6d2b a19eaec@localho st.talkaboutpro gramming.com...[color=blue]
                  > I have tried all different combinations as suggested in this thread. Non
                  > of them works.
                  >
                  > Please try this demo page, and see the source:
                  >
                  > http://www.homesillustratedabq.com/jstests.php
                  >[/color]

                  "myfunc3()" was close but it appended not incremented.

                  Will this work for you? Watch for word-wrap.

                  <html>
                  <head>
                  <title>jstests. html</title>
                  <script>
                  function myfunc9(form,nu mb) {
                  var valu = parseInt(docume nt.forms[form].elements[numb].value,10);
                  valu++;
                  document.forms[form].elements[numb].value = valu;
                  }
                  </script>
                  </head>
                  <body>
                  <form name="form9" style="margin:0 ">
                  Test9: &nbsp;
                  <input type="text" name="number" value="0">
                  <input type="button" value="click"
                  onClick="myfunc 9('form9','numb er')">
                  </form>
                  </body>
                  </html>


                  Comment

                  • Lee

                    #10
                    Re: referencing javascript form object

                    phpcode said:[color=blue]
                    >
                    >I have tried all different combinations as suggested in this thread. Non
                    >of them works.
                    >
                    >Please try this demo page, and see the source:
                    >
                    >http://www.homesillustratedabq.com/jstests.php
                    >[/color]

                    None of those matches my suggestion. Here are two versions:

                    <html>
                    <head>
                    <title>demo</title>

                    <script type="text/javascript">

                    function myfunc1(field) {
                    field.value++;
                    }

                    function myfunc2(formNam e,fieldName) {
                    document.forms[formName].elements[fieldName].value++;
                    }

                    </script>
                    <body>

                    <form name="frank">
                    Test 1: <input name="iris" value="1">
                    <input type="button"
                    onclick="myfunc 1(this.form.iri s)" value="go">
                    <br>
                    Test 2: <input name="irene" value="2">
                    <input type="button"
                    onclick="myfunc 2('frank','iren e')" value="go">
                    </form>
                    </body>
                    </html>

                    Comment

                    • Robert

                      #11
                      Re: referencing javascript form object

                      If you think you will be programming in Javascript some more, you need
                      a good reference. Many folks in this group recommand:

                      Javascript: The Definitive Guide by David Flanagan

                      Robert

                      Comment

                      • fancyydk

                        #12
                        Re: referencing javascript form object

                        instead of passing the name of the form as an argument, just use the
                        form itself. for example
                        if the name of the form is form1, then you can access it with
                        document.form1 right?
                        then
                        var formArg = document.form1;
                        myfunction(form Arg);
                        and also formField in the same manner.
                        if you use this method in a form, perhaps as a response to the user's
                        clicking on a button, you could say
                        onclick="myfunc tion(this.form) ;"
                        that's how I work with forms. Hope this helps

                        Comment

                        • phpcode

                          #13
                          Re: referencing javascript form object

                          I know this.

                          My problem had to do with doing math operations. I didn't have a problem
                          when values are strings.

                          Comment

                          • phpcode

                            #14
                            Re: referencing javascript form object

                            I do have the O'Reilly book.

                            The problem I presented here had to do with incrementing an integer value,
                            not just a simple case of accessing a form field.

                            Comment

                            • phpcode

                              #15
                              Re: referencing javascript form object

                              Thanks McKirahan. Your example worked.



                              Comment

                              Working...