Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issues getting Form Name when input name Equals "name"

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
I'm trying to setup a validator, however i've run into an issue. When I reference formObject.name, I would expect to return the name of that form (<form name="myForm">).

Well, this isn't the case when you have an input field with the name of "name" (<input name="name"). In this case, it returns the InputElement inside the formObject.

To work around this issue, I went with formObject.getAttribute('name'). This worked fine with Firefox, however IE is still returning the HTMLInputElement "name". Is there a better/different way to get the name of an HTML form. Here's the process I'm using:
Code:
forms = document.getElementsByTagName('form');
for(var i=0; i < forms.length; i++){
    form_name = forms[i].getAttribute('name');
}

Thanks!
 
You could hack it by temporarily change the name of the 'name' element (if it exists), then reading the form name, then changing the name back, something like:

Code:
forms = document.getElementsByTagName('form');
for(var loop=0, max=forms.length; loop<max; loop++) {
	var frm = forms[loop];
	var frmName = '';
	var nameEl = forms.elements['name'];
	if (nameEl) {
		nameEl.name = 'TEMPname';
		frmName = frm.name;
		nameEl.name = 'name';
	} else {
		frmName = frm.name;
	}

	alert(frmName);
}

(NOTE: untested code).

However, you're far, far, far better off simply not using the name 'name' in the first place. Clearly it's causing issues, and that should be enough to put any good developer off using it.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
No, I completely understand the importance of not using reserved names, however I'm writing a script to distribute and I foresee people using it for a simple contact form and using "name" as a field name. I tried many methods similar to yours, but even still it would reference the name field below the form. I actually found a solution using this:

Code:
form_name = document.forms[i].cloneNode(false).getAttribute('name');

That does the trick. Thanks for your input.
 
I completely understand the importance of not using reserved names, however I'm writing a script to distribute and I foresee people using it for a simple contact form and using "name" as a field name.

Why not use "forename" and "surname" and break it into two? Give people what you want them to have, and they probably won't complain. Or use "personName" if you need one field. I think using "name" is just asking for trouble.

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi

Dan said:
I think using "name" is just asking for trouble.
I agree. And while some forum/blog/guest book spam bots are looking for meaningful [tt]input[/tt] names, I started to use "foo", "bar", "bla" and alike. Although this is less important for fields without format validation.

Feherke.
 
I'm pretty sure to validate XHTML 1.0 strict, you can't use name in a <form>, only id.

[monkey][snake] <.
 
Well, the script is 100% javascript. It will attach to a form created by the user.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top