Can you change the name of a formfield?

W

Wim Roffal

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

Thanks,
Wim
 
W

Wim Roffal

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
 
R

RIck Measham

Wim said:
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
 
W

Wim Roffal

RIck Measham said:
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
 
G

Grant Wagner

Wim said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top