Can you change the name of a formfield?

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

    Can you change the name of a formfield?

    Is it possible to use javascript to change the name of a field in a form?

    Thanks,
    Wim


  • Wim Roffal

    #2
    Re: Can you change the name of a formfield?

    I did some experiments (in IE 5.5) and they give a rather dubious result.

    Say I have the form MyForm and the inputfield MyInput.

    Now I can change the name of the field. For example by saying
    MyForm.MyInput. name = 'NewName';

    When I submit the form the field will indeed have the new name. So far so
    good. However, when I try to change the value of the field with
    MyForm.NewName. value = 'something';
    I have a problem. This will not work. But when I use the old name it will
    work. The same thing applies when I use getElementsByNa me().

    It this a design error? How should I explain this?

    Wim





    "Wim Roffal" <wimroffelpleas e@nospamm-planet.nl> schreef in bericht
    news:bod7l3$b0h $1@reader10.wxs .nl...[color=blue]
    > Is it possible to use javascript to change the name of a field in a form?
    >
    > Thanks,
    > Wim
    >
    >[/color]


    Comment

    • RIck Measham

      #3
      Re: Can you change the name of a formfield?

      Wim Roffal wrote:
      [color=blue]
      > I did some experiments (in IE 5.5) and they give a rather dubious result.
      >
      > Say I have the form MyForm and the inputfield MyInput.
      >
      > Now I can change the name of the field. For example by saying
      > MyForm.MyInput. name = 'NewName';
      >
      > When I submit the form the field will indeed have the new name. So far so
      > good. However, when I try to change the value of the field with
      > MyForm.NewName. value = 'something';
      > I have a problem. This will not work. But when I use the old name it will
      > work. The same thing applies when I use getElementsByNa me().[/color]

      AFAIK:
      When you talk about a form element as MyForm.MyInput you're talking about
      the id, not the name. However if you've not set an id, the browser will
      assume you want to use the name attribute. As you've seen, you can change
      the name, but you cannot change the id. You've seen that submitting gets
      the new name, so it's just in the page-level javascript that you need to
      always refer to the element by its id.

      Cheers!
      Rick



      --
      Obviously the reply-to is a fake. Just change the 'spam-' to 'i' so that the
      result sounds like why you go to an optometerist.

      Comment

      • Wim Roffal

        #4
        Re: Can you change the name of a formfield?


        "RIck Measham" <rickm@spam-site.net.au> schreef in bericht
        news:3faa48bf$0 $3499$afc38c87@ news.optusnet.c om.au...[color=blue]
        > Wim Roffal wrote:
        >[color=green]
        > > I did some experiments (in IE 5.5) and they give a rather dubious[/color][/color]
        result.[color=blue][color=green]
        > >
        > > Say I have the form MyForm and the inputfield MyInput.
        > >
        > > Now I can change the name of the field. For example by saying
        > > MyForm.MyInput. name = 'NewName';
        > >
        > > When I submit the form the field will indeed have the new name. So far[/color][/color]
        so[color=blue][color=green]
        > > good. However, when I try to change the value of the field with
        > > MyForm.NewName. value = 'something';
        > > I have a problem. This will not work. But when I use the old name it[/color][/color]
        will[color=blue][color=green]
        > > work. The same thing applies when I use getElementsByNa me().[/color]
        >
        > AFAIK:
        > When you talk about a form element as MyForm.MyInput you're talking about
        > the id, not the name. However if you've not set an id, the browser will
        > assume you want to use the name attribute. As you've seen, you can change
        > the name, but you cannot change the id. You've seen that submitting gets
        > the new name, so it's just in the page-level javascript that you need to
        > always refer to the element by its id.
        >
        > Cheers!
        > Rick[/color]

        Thanks for your reply. The funny thing is that my testfield had also an id.
        That is just an alternative: you can use both MyForm.MyInput and
        MyForm.MyId.

        cheers,
        Wim


        Comment

        • Grant Wagner

          #5
          Re: Can you change the name of a formfield?

          Wim Roffal wrote:
          [color=blue]
          > Is it possible to use javascript to change the name of a field in a form?
          >
          > Thanks,
          > Wim[/color]

          I'm not sure why you'd need to do such a thing. There are a myriad of other
          alternative solutions that don't require the ability to change the name of
          form element.

          For example, if the name is one of a set of known values, then simply have a
          hidden input for each known name and stick the value in the appropriate
          input.

          If there are many possible names, or the name is not known at the time the
          form is loaded, then replace the concept of a named form element with two
          elements, one containing the "name", one containing the "value". Once
          submitted to the server, it can make the necessary connection:

          <input type="hidden" name="elementNa me" value="defaultN ame" />
          <input type="hidden" name="elementVa lue" value="defaultV alue" />

          Lastly, if you really need to do this (and I'm still not convinced there
          isn't an alternative solution) then you could simply create the element on
          the fly and name it whatever you want:

          var elem = document.create Element("input" );
          elem.type = "hidden";
          elem.name = "myInputNam e";
          elem.id = "myInputId" ;
          elem.value = "theValue";
          document.forms['theForm'].appendChild(el em);

          The code above will need to be protected against being run on browsers which
          do not understand or implement document.create Element() and appendChild()
          properly.

          --
          | Grant Wagner <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available at:
          *


          * Internet Explorer DOM Reference available at:
          *
          Gain technical skills through documentation and training, earn certifications and connect with the community


          * Netscape 6/7 DOM Reference available at:
          * http://www.mozilla.org/docs/dom/domref/
          * Tips for upgrading JavaScript for Netscape 7 / Mozilla
          * http://www.mozilla.org/docs/web-deve...upgrade_2.html


          Comment

          Working...