Form field with same name as js form property--workaround?

R

Ronaldo N

I'm trying to do some javascript form validation and I've discovered a
rather difficult situation to handle with IE.

Let's say there's a form with three input fields named "name",
"method", or "length". Whenever my javascript tries to access the
form's name or length, and the form happens to have input fields named
"name" and "length", I'm actually accessing the input fields and there
seems to be no way to access those properties of the form.

I have an easy workaround for form.length, so there's no need to give
me a workaround for that one, but there's a bigger issue with fields
like name, method, action, etc.

For example, using document.forms[0].name to get the name of the form
seems to map to document.forms[0].elements["name"] istead of the
actual name of the form. Is there a work around for this? I am
writing some generic form validation code, and I can't expect the
person implementing my form validation code to avoid using "name" and
"length" as names for their form fields.

Thanks in advance,
RN
 
M

Martin Honnen

Ronaldo said:
I'm trying to do some javascript form validation and I've discovered a
rather difficult situation to handle with IE.

Let's say there's a form with three input fields named "name",
"method", or "length". Whenever my javascript tries to access the
form's name or length, and the form happens to have input fields named
"name" and "length", I'm actually accessing the input fields and there
seems to be no way to access those properties of the form.

I have an easy workaround for form.length, so there's no need to give
me a workaround for that one, but there's a bigger issue with fields
like name, method, action, etc.

For example, using document.forms[0].name to get the name of the form
seems to map to document.forms[0].elements["name"] istead of the
actual name of the form. Is there a work around for this? I am
writing some generic form validation code, and I can't expect the
person implementing my form validation code to avoid using "name" and
"length" as names for their form fields.

As long as you are looking for HTML attributes like action or method or
name with Mozilla you can use
formObject.getAttribute('action')
formObject.getAttribute('method')
formObject.getAttribute('name')
however with IE (at least IE/Win) that doesn't help as IE somehow
confuses HTML attributes and DOM properties and fails to return the HTML
attribute value if the form element object has a property of the same name.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top