Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can you change the name of a formfield?

Reply
Thread Tools

Can you change the name of a formfield?

 
 
Wim Roffal
Guest
Posts: n/a
 
      11-06-2003
Is it possible to use javascript to change the name of a field in a form?

Thanks,
Wim


 
Reply With Quote
 
 
 
 
Wim Roffal
Guest
Posts: n/a
 
      11-06-2003
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 getElementsByName().

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

Wim





"Wim Roffal" <> schreef in bericht
news:bod7l3$b0h$...
> Is it possible to use javascript to change the name of a field in a form?
>
> Thanks,
> Wim
>
>



 
Reply With Quote
 
 
 
 
RIck Measham
Guest
Posts: n/a
 
      11-06-2003
Wim Roffal wrote:

> 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 getElementsByName().


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.
 
Reply With Quote
 
Wim Roffal
Guest
Posts: n/a
 
      11-06-2003

"RIck Measham" <> schreef in bericht
news:3faa48bf$0$3499$ ...
> Wim Roffal wrote:
>
> > 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 getElementsByName().

>
> 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


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


 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      11-10-2003
Wim Roffal wrote:

> Is it possible to use javascript to change the name of a field in a form?
>
> Thanks,
> Wim


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="elementName" value="defaultName" />
<input type="hidden" name="elementValue" value="defaultValue" />

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.createElement("input");
elem.type = "hidden";
elem.name = "myInputName";
elem.id = "myInputId";
elem.value = "theValue";
document.forms['theForm'].appendChild(elem);

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

--
| Grant Wagner <>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using forms authentication, but you can get at a file if you know the name of it. Mufasa ASP .Net 4 05-01-2007 02:55 PM
how can i retrieve the name of form when one of the element name in this form is 'name' ashok.dhananjeyan@gmail.com Javascript 6 11-24-2006 02:54 AM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments