By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,551 Members | 2,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,551 IT Pros & Developers. It's quick & easy.

how can i retrieve the name of form when one of the element name in this form is 'name'

P: n/a
Hi,

Actually , I wrote one javascript to retrieve the name of the form in
one particular page.
what i did in this page is,

<script>

function checkbutton()
{
var f = document.forms("myform");

alert(f) ;

alert(f.name) ;

alert(f.name.value) ;

}
</script>
<html>

<body>

<form name="myform" >

Click Here to check the value of name button
<input type=button name='name' value='Click Here'
onclick='checkbutton();' >
</form>
</body>

</html>

but i can not able to retrieve the name of the form , only i get the
object of element.so how can i get the form name.

there is any other way to get the form name. or element name cannot be
a 'name'.

Please reply me ASAP.
Regards
Ashok D.

Nov 23 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
as***************@gmail.com said the following on 11/22/2006 11:47 PM:
Hi,

Actually , I wrote one javascript to retrieve the name of the form in
one particular page.
Trivial. Was there a question?
what i did in this page is,

<script>

function checkbutton()
{
var f = document.forms("myform");

alert(f) ;

alert(f.name) ;

alert(f.name.value) ;

}
Ok. Is there a question?
</script>
<html>

<body>

<form name="myform" >

Click Here to check the value of name button
<input type=button name='name' value='Click Here'
onclick='checkbutton();' >
</form>
</body>

</html>

but i can not able to retrieve the name of the form , only i get the
object of element.so how can i get the form name.
Change the name of your input to something besides name that isn't in
the forms collection.
there is any other way to get the form name. or element name cannot be
a 'name'.
Rename your input.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '06 #2

P: n/a
as***************@gmail.com wrote:
[...]
there is any other way to get the form name. or element name cannot be
a 'name'.
In Firefox and Opera you can use getAttribute, but since that doesn't
work for IE you are back to what Randy said - don't give any element a
name of "name".

<form name="Fred">
<input type="button" name="name" value="Show form name"
onclick="alert(this.form.getAttribute('name'));">
</form>


--
Rob

Nov 23 '06 #3

P: n/a
VK

as***************@gmail.com wrote:
Actually , I wrote one javascript to retrieve the name of the form in
one particular page.
<form name="myform" >

Click Here to check the value of name button
<input type=button name='name' value='Click Here'
onclick='checkbutton();' >
</form>
As it was already suggested, there are two forbidden names for form
elements you *never ever* use unless with a gun against your temple:
"name" and "submit". Each of them means huge scripting troubles in the
immediate future.

But presuming that you have to deal with a 3rd party form and there is
absolutely no way to insist on server-side changes...

Normally when you are using DOM 0 methods the engine understands that
you are willing to reach form elements and when you are using
getAttribute then the engine understands that you want to reach element
attributes. That works for all browsers except IE which presumes by
default that all users are idiots and what they asking for is not what
they really want (thus on getAttribute it still returns element from
form collection). This way the task is to bypass somehow this "parasite
neuristic". They way we are suggesting to our clients is:

....
// Please note that DOM 0 uses squared brakets
// notation, not parenthesis like in your original code.
// Otherwise it will break out on any normal browser:
var f = document.forms["myform"];

alert(f); // form object

alert(f.attributes.getNamedItem('name').nodeValue) // form name;

alert(f.elements['name'].value) ;

Nov 23 '06 #4

P: n/a
VK said the following on 11/23/2006 6:54 AM:
as***************@gmail.com wrote:
> Actually , I wrote one javascript to retrieve the name of the form in
one particular page.
<form name="myform" >

Click Here to check the value of name button
<input type=button name='name' value='Click Here'
onclick='checkbutton();' >
</form>

As it was already suggested, there are two forbidden names for form
elements you *never ever* use unless with a gun against your temple:
"name" and "submit". Each of them means huge scripting troubles in the
immediate future.
Congratulations, you just named 2 of the 100 or so name/IDs that come to
mind as potentially causing a problem. Try using dot notation while
naming your form "links", or "images" or......

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '06 #5

P: n/a
VK
As it was already suggested, there are two forbidden names for form
elements you *never ever* use unless with a gun against your temple:
"name" and "submit". Each of them means huge scripting troubles in the
immediate future.

Congratulations, you just named 2 of the 100 or so name/IDs that come to
mind as potentially causing a problem. Try using dot notation while
naming your form "links", or "images" or......
I listed two names *for form elements* (not forms themselves) which are
causing problems with *any* notation. Maybe I should say "controls
inside the form" to avoid possible confusion. Yet the form collection
is called "elements" and the whole context (with code provided) was
very clear... OK, rather than arguing::

"There are two forbidden names for controls inside your form which you
*never ever* use unless with a gun against your temple: "name" and
"submit" (all in lower case). Each of them means huge scripting
troubles in the immediate future."

Nov 23 '06 #6

P: n/a
VK wrote:
As it was already suggested, there are two forbidden names for form
elements you *never ever* use unless with a gun against your temple:
"name" and "submit". Each of them means huge scripting troubles in the
immediate future.
Congratulations, you just named 2 of the 100 or so name/IDs that come to
mind as potentially causing a problem. Try using dot notation while
naming your form "links", or "images" or......

I listed two names *for form elements* (not forms themselves)
I think you mean form controls.
which are
causing problems with *any* notation. Maybe I should say "controls
inside the form" to avoid possible confusion.
To be a control, an element *must* be inside the form (and have a name
attribute). To avoid confusion, call them form controls.
Yet the form collection
is called "elements" and the whole context (with code provided) was
very clear...
The collection of forms is called "forms", the collection of a
particular form's controls is called "elements". Your post wasn't
clear about that at all.

Note to W3C: why wasn't the collection of controls called "controls"?
OK, rather than arguing::
"There are two forbidden names ... "name" and "submit"
And *any other property* that a form might have, such as reset,
nodeName, nodeValue, nodeType, prefix, localName, tagName, title, lang,
action, method, etc. As Randy said, there are about 100 of them (IE
has over 130). It is one reason to recommend the use of the more
formal forms[formName].elements[elementName] over simpler dot property
access.
--
Rob

Nov 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.