472,796 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 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 2602
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" , ""...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.