473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Read form "name" attribute when it contains form field with name "name"

Hello,

I have bumped upon this problem: I do some client-side form processing
with JavaScript, and for this I loop over all the forms in the
document. In order to identify them, I read their "name" property
(which sources from "name" HTML attribue). The problem is, that if the
form contains form control named "name", it overwrites the form name
property. In fact, I'm quite surprised that it's so easy to spoil any
of the form object properties - the form just needs to contain a field
named, say, "onsubmit", and - voila - it's done! Also, if the form
contains field "name", then attempting to assign another name to the
form object will result in error. Quite lame, if you ask me... The
field names shouldn't really matter.
Is there any other alternative way to read and write the form object
properties, if their name conflicts with the form field names?

Thank you,

Pavils
Jul 23 '05 #1
3 2660
Pavils Jurjans wrote:
Hello,

I have bumped upon this problem: I do some client-side form processing
with JavaScript, and for this I loop over all the forms in the
document. In order to identify them, I read their "name" property
(which sources from "name" HTML attribue). The problem is, that if the
form contains form control named "name", it overwrites the form name
property. In fact, I'm quite surprised that it's so easy to spoil any
of the form object properties - the form just needs to contain a field
named, say, "onsubmit", and - voila - it's done! Also, if the form
contains field "name", then attempting to assign another name to the
form object will result in error. Quite lame, if you ask me... The
field names shouldn't really matter.
Is there any other alternative way to read and write the form object
properties, if their name conflicts with the form field names?

Thank you,

Pavils


Hi Pavils,

It only happens when you read over all properties you can find in the
document.
Bad habbit.

Try looping over this:
document.forms

it returns zero forms, one form, OR an array of forms.

In case of an array:
oneForm = document.forms[X];

where X is an index. (0 for first form, 1 for second, etc)

from there on:
oneForm.name.value will return the value of the element with the name "name"
in the specified form.

Hope that helps.

Regards,
Erwin Moller
Jul 23 '05 #2
Pavils Jurjans wrote:

[snip]
In order to identify [the forms], I read their "name" property
(which sources from "name" HTML attribue). The problem is, that if the
form contains form control named "name", it overwrites the form name
property.
This can happen in any situation where the object model for a user
agent exposes elements via some kind of shortcut. Forms and images can
be referenced directly through the document object. Form controls
through a form object. Microsoft also think it a good idea to
reference elements using their id values through the global object.
In fact, I'm quite surprised that it's so easy to spoil any
of the form object properties
I personally think it's a mistake on the part of the browser vendors.
Note that none of this behaviour is standardised, but parts have
become a de facto standard.

[snip]
Is there any other alternative way to read and write the form object
properties, if their name conflicts with the form field names?


There are two solutions, but neither are necessarily what you're
looking for.

1) Don't use the name attribute to identify the form. That
mechanism was labeled a compatibility feature a long time
ago. For instance, XHTML Strict has removed the name attribute
from a number of elements, including FORM (but not form
controls). Instead, use the id attribute. Of course, you'll
still run foul of the problem if you have an 'id' control.
2) Change the capitalisation of the form control. ECMAScript is,
after all, case-sensitive.

If IE isn't a concern, which isn't likely, a third possible solution
which would require changes to neither your mark-up nor server-side
code is the use of the getAttribute method. However, I haven't tested
whether that's viable across a range of user agents (though it does
work in Firefox).

The reason why it doesn't work in IE (and Opera, since it tries to
emulate some of IE's object model) is that Microsoft decided to flout
the W3C DOM again and allow getAttribute to return anything (function
references to listeners, object references to elements, etc.) rather
than just the string values of attributes.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Hi Michael,

Thanks for understanding the issue and writing through answer. I was
thinking about using the getAttribute, too, but found out that it's
not behaving as normal XML method, but is actually repeating the DOM
call.

Just so that noone thinks I'm doing something wrong (ie, Erwins
comment was calling a bad habbit something I don't do, plus providing
incorrect info on document.forms. Sometimes it helps to read
carefully.), here's a code for testing:

<html>
<head>
<title> Form propery reading </title>

<script>
function runMe() {
var f = document.forms[0];
var ptyName = "action"; // "name"

alert(f[ptyName]);
alert(f.getAttribute(ptyName));
}
</script>

</head>

<body onload="runMe()">

<form name="MyForm" action="HereWeGo.asp">
<input type="text" name="name" value="MyTextField"/><br/>
<input type="text" name="action" value="MyActionField"/>
</form>

<input type="button" onclick="runMe()" value="Test"/>

</body>
</html>
As you could see, whenever you have a form control named just like one
of about 130 properties of the form object in DOM (it's in MSIE6.
There are about 100 in Firefox.), you make it impossible to read the
corresponding form property value, because silly shortcuts to from
controls plainly overwrites it. Very stupid, and the worst thing is
noone is about to correct this OOP violation. Luckily, Firefox doesn't
spoil the getAttribute method, but that doesn't help in MSIE case.

Pavils.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Davis | last post by:
I want to know what's the differences between Request.Form("Field Name") and Request.QueryString("Field Name") OR they function exactly the same, which is to return the value of the field?? ...
6
by: Java script Dude | last post by:
We just discovered another IE bug. When an html form contains an element with a name of `name` IE's internal index screws up the .name property of the containing form to point to the bad element...
4
by: Marc Elser | last post by:
Hi Everybody, Can someone please tell me how to access the form name if there's a form field named "name", for example: <form name="myform"> <input type="text" name="name" value="Marc">...
14
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
0
by: AA Arens | last post by:
I made a find record button on my form "company" (with field for name, phone nr, e-mail etc.). I expect that when I focus on one of the field, the "look in" of the find-record dialog box should...
6
by: Dick Watson | last post by:
I had a page that works when setup like this: === <form name="frmCalc" action=""> <script type="text/javascript"> function btnCalc_onclick(abc) { return "got here with " + abc; }
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
9
by: scripteaze | last post by:
Im using mechanize method for retrieving the form so that i may log into it. I need to find a way to get the form name. Its not listed anywhere in the html source.The reason i need to do this is...
3
by: libish | last post by:
hi all.. can any one help me by suggesting an idea to take the attribute "name" from a node. <r t="error" a="active" i=""> here t , a , i are the attribute name and "error" , "active" , ""...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.