dynamic formname/fieldname

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

    dynamic formname/fieldname

    Hello,

    I was wondering if I could do the following : can I change the formname and
    fieldname with an argument? This way I can re-use the script multiple times.

    My code (that doesn't work):

    -----------------
    <script>
    function test(arg,arg2) {
    document.arg.ar g2.value="yes";
    }
    </script>

    <form name="frmtest">
    <input type="text" name="test1" value="hello">
    <input type="button" onclick="javasc ript:test('frmt est','test1');" >
    </form>
    -----------------

    How can I make this work?
    Thanks in advance!


  • Grunken

    #2
    Re: dynamic formname/fieldname

    This should work :o)

    <script type="text/javascript">
    function test(arg,arg2) {
    document.forms[arg][arg2].value="yes";
    }
    </script>

    Best Regards

    Nick (grunken.dk)

    P.S. Sorry for doubleposts, I have never used Usenet before :o)

    "geradeaus" <gehegeradeaus@ hotmail.com> wrote in message
    news:p9shc.601$ ZE6.72333450@he stia.telenet-ops.be...[color=blue]
    > Hello,
    >
    > I was wondering if I could do the following : can I change the formname[/color]
    and[color=blue]
    > fieldname with an argument? This way I can re-use the script multiple[/color]
    times.[color=blue]
    >
    > My code (that doesn't work):
    >
    > -----------------
    > <script>
    > function test(arg,arg2) {
    > document.arg.ar g2.value="yes";
    > }
    > </script>
    >
    > <form name="frmtest">
    > <input type="text" name="test1" value="hello">
    > <input type="button" onclick="javasc ript:test('frmt est','test1');" >
    > </form>
    > -----------------
    >
    > How can I make this work?
    > Thanks in advance!
    >
    >[/color]


    Comment

    • mscir

      #3
      Re: dynamic formname/fieldname

      geradeaus wrote:[color=blue]
      > Hello,
      > I was wondering if I could do the following : can I change the formname and
      > fieldname with an argument? This way I can re-use the script multiple times.[/color]
      [color=blue]
      > <script>
      > function test(arg,arg2) {
      > document.arg.ar g2.value="yes";
      > }
      > </script>
      >
      > <form name="frmtest">
      > <input type="text" name="test1" value="hello">
      > <input type="button" onclick="javasc ript:test('frmt est','test1');" >
      > </form>[/color]

      Try this approach:
      - make sure you declare the type of script correctly.
      - arg will always have to be a form
      - arg2 will always have to be an element of the arg form

      <script type="text/javascript">
      function test(arg,arg2) {
      document.forms[arg].elements[arg2].value="yes";
      }
      </script>

      Mike

      Comment

      • Michael Winter

        #4
        Re: dynamic formname/fieldname

        On Wed, 21 Apr 2004 12:56:20 +0200, Grunken <grunkendk@hotm ail.com> wrote:
        [color=blue]
        > This should work :o)
        >
        > <script type="text/javascript">
        > function test(arg,arg2) {
        > document.forms[arg][arg2].value="yes";
        > }
        > </script>[/color]

        The more usual way is

        document.forms[ arg ].elements[ arg2 ].value = ...;

        though yours certainly works on my browsers.
        [color=blue]
        > P.S. Sorry for doubleposts, I have never used Usenet before :o)[/color]

        A quick tip: don't top-post and quote verbatim. The preferred way to post
        is to remove parts of the post (usually indicated with [snip] or similar)
        that don't apply to your response, and reply under the remaining sections
        so readers can understand exactly what you're answering.

        This is especially important if the poster has made several points: one
        long reply might be difficult to associate with each destinctive issue.

        Welcome to Usenet, :)
        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Michael Winter

          #5
          Re: dynamic formname/fieldname

          On Wed, 21 Apr 2004 10:37:09 GMT, geradeaus <gehegeradeaus@ hotmail.com>
          wrote:
          [color=blue]
          > I was wondering if I could do the following : can I change the formname
          > and fieldname with an argument? This way I can re-use the script
          > multiple times.
          >
          > My code (that doesn't work):[/color]

          In addition to what others have said...
          [color=blue]
          > -----------------
          > <script>
          > function test(arg,arg2) {
          > document.arg.ar g2.value="yes";
          > }
          > </script>
          >
          > <form name="frmtest">
          > <input type="text" name="test1" value="hello">
          > <input type="button" onclick="javasc ript:test('frmt est','test1');" >[/color]

          The "javascript :" above only means something in Internet Explorer. In
          other browsers, it is simply a label. You can remove it.
          [color=blue]
          > </form>
          > -----------------
          >
          > How can I make this work?[/color]

          You can apply a slight simplification. In intrinsic event handlers, such
          as onclick, the this operator refers to the element that contains the
          handler. For example,

          <button type="button" value="Hello"
          onclick="alert( this.value)">Te st</button>

          clicking the button above will display, "Hello".

          Form controls have a property, form, that contains a reference to their
          containing FORM element. You can use the technique above to simply getting
          the form reference:

          <script type="text/javascript">
          function test( form, control ) {
          form.elements[ control ].value = "yes";
          }
          </script>
          ...
          <form name="frmtest">
          <input type="text" name="test1" value="hello">
          <input type="button" onclick="test(t his.form,'test1 ');">
          </form>

          Good luck,
          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • geradeaus

            #6
            Re: dynamic formname/fieldname


            "Michael Winter" <M.Winter@bluey onder.co.invali d> schreef in bericht
            news:opr6s4ovk4 5vklcq@news-text.blueyonder .co.uk...[color=blue]
            > On Wed, 21 Apr 2004 10:37:09 GMT, geradeaus <gehegeradeaus@ hotmail.com>
            > wrote:
            >[color=green]
            > > I was wondering if I could do the following : can I change the formname
            > > and fieldname with an argument? This way I can re-use the script
            > > multiple times.
            > >
            > > My code (that doesn't work):[/color]
            >
            > In addition to what others have said...
            >[color=green]
            > > -----------------
            > > <script>
            > > function test(arg,arg2) {
            > > document.arg.ar g2.value="yes";
            > > }
            > > </script>
            > >
            > > <form name="frmtest">
            > > <input type="text" name="test1" value="hello">
            > > <input type="button" onclick="javasc ript:test('frmt est','test1');" >[/color]
            >
            > The "javascript :" above only means something in Internet Explorer. In
            > other browsers, it is simply a label. You can remove it.
            >[color=green]
            > > </form>
            > > -----------------
            > >
            > > How can I make this work?[/color]
            >
            > You can apply a slight simplification. In intrinsic event handlers, such
            > as onclick, the this operator refers to the element that contains the
            > handler. For example,
            >
            > <button type="button" value="Hello"
            > onclick="alert( this.value)">Te st</button>
            >
            > clicking the button above will display, "Hello".
            >
            > Form controls have a property, form, that contains a reference to their
            > containing FORM element. You can use the technique above to simply getting
            > the form reference:
            >
            > <script type="text/javascript">
            > function test( form, control ) {
            > form.elements[ control ].value = "yes";
            > }
            > </script>
            > ...
            > <form name="frmtest">
            > <input type="text" name="test1" value="hello">
            > <input type="button" onclick="test(t his.form,'test1 ');">
            > </form>
            >
            > Good luck,
            > Mike
            >
            > --[/color]

            Thanks for the advice!


            Comment

            • Grunken

              #7
              Re: dynamic formname/fieldname

              Thanks Mike :o)

              Your reply was very helpfull, and I will remember you'r advice !

              I have used different forums for a long time, but never usenet because it's
              a lot easyer to help peoble with scripts, who can write the way I do
              (danish) :o)

              Now I have to upgrade my english writing to the next stage, thats why I
              selected USENET :o)

              So if you can't get the meaning of what I'm writing - I know why :o)

              Regards

              Nick


              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
              news:opr6s35dtd 5vklcq@news-text.blueyonder .co.uk...[color=blue]
              > On Wed, 21 Apr 2004 12:56:20 +0200, Grunken <grunkendk@hotm ail.com> wrote:
              >[color=green]
              > > This should work :o)
              > >
              > > <script type="text/javascript">
              > > function test(arg,arg2) {
              > > document.forms[arg][arg2].value="yes";
              > > }
              > > </script>[/color]
              >
              > The more usual way is
              >
              > document.forms[ arg ].elements[ arg2 ].value = ...;
              >
              > though yours certainly works on my browsers.
              >[color=green]
              > > P.S. Sorry for doubleposts, I have never used Usenet before :o)[/color]
              >
              > A quick tip: don't top-post and quote verbatim. The preferred way to post
              > is to remove parts of the post (usually indicated with [snip] or similar)
              > that don't apply to your response, and reply under the remaining sections
              > so readers can understand exactly what you're answering.
              >
              > This is especially important if the poster has made several points: one
              > long reply might be difficult to associate with each destinctive issue.
              >
              > Welcome to Usenet, :)
              > Mike
              >
              > --
              > Michael Winter
              > M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)[/color]


              Comment

              Working...