473,473 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Finding a Form Element from an Input

Hi, if I had the ID of an input element, how can I find the input's
FORM in javascript?

Basically, given a input's dom id, I want to insert something in the
onSubmit of the Form that that input is in. I want to avoid making the
user have to submit the form's id if possible.

Let me know if my question is unclear...thanks!

Kel

Jul 30 '06 #1
14 2001
wrote on 30 jul 2006 in comp.lang.javascript:
Hi, if I had the ID of an input element, how can I find the input's
FORM in javascript?
<form id=f name=ff>
<input id=x>
</form>

<script type='text/javascript'>
var x = document.getElementById('x')
alert(x.form.id) // returns f
alert(x.form.name) // returns ff
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 30 '06 #2
Nice...Silly of me to not try just viewing the properties of the input
element in a debugger! Thanks :)

Kel

Evertjan. wrote:
wrote on 30 jul 2006 in comp.lang.javascript:
Hi, if I had the ID of an input element, how can I find the input's
FORM in javascript?

<form id=f name=ff>
<input id=x>
</form>

<script type='text/javascript'>
var x = document.getElementById('x')
alert(x.form.id) // returns f
alert(x.form.name) // returns ff
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 31 '06 #3
"Evertjan." <ex**************@interxnl.netwrites:
<form id=f name=ff>
Just to be pedantic (can't let an error go uncommented :)
the "name" and "id" attributes of a "form" element must be
identical in valid HTML.
<URL:http://www.w3.org/TR/html4/struct/links.html#h-12.2.3>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 1 '06 #4
Lasse Reichstein Nielsen wrote on 30 jul 2006 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.netwrites:
><form id=f name=ff>

Just to be pedantic (can't let an error go uncommented :)
the "name" and "id" attributes of a "form" element must be
identical in valid HTML.
<URL:http://www.w3.org/TR/html4/struct/links.html#h-12.2.3>
Ah, yes, "valid HTML".

But does the rule have a reason beond semantics and
is it implemented on a mainstream browser?

[Personally I could do with loosing "name" alltogether
and using only "id", so agreeing with w3's feelings.]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 1 '06 #5
On 01/08/2006 09:58, Evertjan. wrote:
Lasse Reichstein Nielsen wrote on 30 jul 2006 in comp.lang.javascript:
[snip]
>...the "name" and "id" attributes of a "form" element must be
identical in valid HTML.
[snip]
But does the rule have a reason beond semantics and
is it implemented on a mainstream browser?
What would you expect a browser to do if it 'implemented' that rule? A
browser could do just about anything when encountering invalid markup. A
slightly more reasonable expectation might have the browser expose the
form in the forms collection by only one of those identifiers, but
that's unlikely happen; it's just as easy to expose both.

The better question to ask is, "Why use different values?" Both
attributes serve to identify a form to a script, though the id attribute
also permits the form to function as a destination anchor. The only
reason to continue using the name attribute is to support obsolete
browsers like NN4 that won't expose the form by id alone. What value
would there be in forms['id-identifier'] and forms['name-identifier'] as
two independent approaches to the same objective, in the same script?

Of course, it's not infrequently the case that both can be omitted when
manipulation of the form is driven by form-related events.

[snip]

Mike
Aug 1 '06 #6
Michael Winter wrote on 01 aug 2006 in comp.lang.javascript:
What value
would there be in forms['id-identifier'] and forms['name-identifier'] as
two independent approaches to the same objective, in the same script?
True.

So can we forget name and use only "id" in modern browsers?

Not in:

<form><input id="Textinput">

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 1 '06 #7
Lasse Reichstein Nielsen <lr*@hotpop.comwrote in
news:4p**********@hotpop.com:
the "name" and "id" attributes of a "form" element must be
identical in valid HTML.
No they don't.

7.5.2
The id attribute shares the same name space as the name attribute _when
used for anchor names_.

http://www.w3.org/TR/html4/struct/global.html#adef-id
Aug 1 '06 #8

Jim Land (NO SPAM) wrote:
Lasse Reichstein Nielsen <lr*@hotpop.comwrote in
news:4p**********@hotpop.com:
the "name" and "id" attributes of a "form" element must be
identical in valid HTML.

No they don't.

7.5.2
The id attribute shares the same name space as the name attribute _when
used for anchor names_.

http://www.w3.org/TR/html4/struct/global.html#adef-id
What about:-

<quote = cite="http://www.w3.org/TR/html4/struct/links.html#h-12.2.3">
It is permissible to use both attributes to specify an element's unique
identifier for the following elements: A, APPLET, FORM, FRAME, IFRAME,
IMG, and MAP. When both attributes are used on a single element, their
values must be identical.
<quote>

- which explicitly stats that it is permissible to use both a NAME and
an ID on FORM elements, but that both must have the same value.

Richard.

Aug 1 '06 #9
On 01/08/2006 14:29, Evertjan. wrote:

[To identify form elements]
So can we forget name and use only "id" in modern browsers?
That's the idea.
Not in:

<form><input id="Textinput">
Yes. The name attribute is still specifies the control name for form
controls. It's also still used with other elements like param and meta.

Mike
Aug 1 '06 #10
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in
news:11**********************@m73g2000cwd.googlegr oups.com:
>
Jim Land (NO SPAM) wrote:
>Lasse Reichstein Nielsen <lr*@hotpop.comwrote in
news:4p**********@hotpop.com:
the "name" and "id" attributes of a "form" element must be
identical in valid HTML.

No they don't.

7.5.2
The id attribute shares the same name space as the name attribute
_when
>used for anchor names_.

http://www.w3.org/TR/html4/struct/global.html#adef-id

What about:-

<quote = cite="http://www.w3.org/TR/html4/struct/links.html#h-12.2.3">
It is permissible to use both attributes to specify an element's unique
identifier for the following elements: A, APPLET, FORM, FRAME, IFRAME,
IMG, and MAP. When both attributes are used on a single element, their
values must be identical.
<quote>

- which explicitly stats that it is permissible to use both a NAME and
an ID on FORM elements, but that both must have the same value.
Well, yes, but your quotation appears as a note in the section on A tags.
If this were really true, one would expect to find the details by
examining each of the tags named. But in fact there is no such
prohibition in the sections which define APPLET, FORM, FRAME, IFRAME,
IMG, or MAP tags.
Aug 1 '06 #11
Jim Land said the following on 8/1/2006 6:15 PM:
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in
news:11**********************@m73g2000cwd.googlegr oups.com:
>Jim Land (NO SPAM) wrote:
>>Lasse Reichstein Nielsen <lr*@hotpop.comwrote in
news:4p**********@hotpop.com:

the "name" and "id" attributes of a "form" element must be
identical in valid HTML.

No they don't.

7.5.2
The id attribute shares the same name space as the name attribute
_when
>>used for anchor names_.

http://www.w3.org/TR/html4/struct/global.html#adef-id
What about:-

<quote = cite="http://www.w3.org/TR/html4/struct/links.html#h-12.2.3">
It is permissible to use both attributes to specify an element's unique
identifier for the following elements: A, APPLET, FORM, FRAME, IFRAME,
IMG, and MAP. When both attributes are used on a single element, their
values must be identical.
<quote>

- which explicitly stats that it is permissible to use both a NAME and
an ID on FORM elements, but that both must have the same value.

Well, yes, but your quotation appears as a note in the section on A tags.

But it clearly says it applies to A, APPLET, FORM, FRAME, IFRAME, IMG
and MAP elements.
If this were really true, one would expect to find the details by
examining each of the tags named.
Are you kidding? Duplicate it all over the place?
But in fact there is no such prohibition in the sections which
define APPLET, FORM, FRAME, IFRAME, IMG, or MAP tags.
In fact, trying to validate a form element with a NAME attribute under
HTML4.01 Strict gets an error from the validator:

Error Line 12 column 11: there is no attribute "NAME".
<form name="myForm" id="myID" action="somewhere">

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 1 '06 #12
On 01/08/2006 23:20, Randy Webb wrote:
Jim Land said the following on 8/1/2006 6:15 PM:
>"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in
news:11**********************@m73g2000cwd.googleg roups.com:
[snip]
>><quote = cite="http://www.w3.org/TR/html4/struct/links.html#h-12.2.3">
It is permissible to use both attributes to specify an element's unique
identifier for the following elements: A, APPLET, FORM, FRAME, IFRAME,
IMG, and MAP. When both attributes are used on a single element, their
values must be identical.
<quote>
[snip]
>Well, yes, but your quotation appears as a note in the section on A tags.
What bearing does that have upon the statement that when "both [name and
id] attributes are used on a single element, their values must be
identical"?

The section (12.2.3 Anchors with the id attribute) notes that both
attributes share the same namespace; two different elements cannot use
either attribute to declare a duplicate identifier. As the name
attribute on the other listed elements acts as an identifier, the same
usage rules apply to them, too: the id and name attribute values must be
identical on the same element, and the resulting identifier must be
unique within a document.

[snip]
>But in fact there is no such prohibition in the sections which define
APPLET, FORM, FRAME, IFRAME, IMG, or MAP tags.
There doesn't need to be. Defining it once is enough.
In fact, trying to validate a form element with a NAME attribute under
HTML4.01 Strict gets an error from the validator:
I'm not sure what you did to reach that conclusion, Randy, but your test
was faulty. Form elements may have name attributes in all 'versions' of
HTML 4. Check the DTDs, if you like.

Versions of XHTML, on the other hand, (probably 1.1+ - I'm not going to
look :-P) have removed the name attribute from many elements (form
controls being an obvious exception).

Mike
Aug 2 '06 #13
Michael Winter said the following on 8/2/2006 5:03 PM:
On 01/08/2006 23:20, Randy Webb wrote:
<snip>
>In fact, trying to validate a form element with a NAME attribute under
HTML4.01 Strict gets an error from the validator:

I'm not sure what you did to reach that conclusion, Randy, but your test
was faulty.
My test was faulty? I merely went to the W3C's validator and attempted
to validate this HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>Blank Test Page</title>
</head>
<body>
<form name="myForm" action="somewhere">
<input id="myInput">
</form>
</body>
</html>
And received three error messages, the first being:

Error Line 7 column 11: there is no attribute "NAME".
<form name="myForm" action="somewhere">

You can validate it for the other 2, they deal with INPUT not being
allowed there and the FORM not being closed. But since the above error
is the first, that is always the first error to correct. Removing the
name attribute and re-validating corrects that error. So, how is my test
"faulty" when the W3C's validator gives me the results I posted?

Or, does that mean the W3C doesn't even know what it's own documentation
says?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 2 '06 #14
On 02/08/2006 22:58, Randy Webb wrote:
Michael Winter said the following on 8/2/2006 5:03 PM:
[snip]
My test was faulty? ...
My apologies. It seems that you chose a document type where form
elements do not have a name attribute. The attribute is also missing
from img elements. In both cases, the attribute was added between the
1998 HTML 4.0 Recommendation and the 1999 HTML 4.01 Proposed
Recommendation. I wasn't aware that such changes took place; I always
thought that the remark that accompanies the attributes in the prose:

Note. This attribute has been included for backwards
compatibility. Applications should use the id attribute to
identify elements.

was a historic remark. Clearly not.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
Incidentally, the formal public identifier and the system identifier are
out-of-sync as the latter identifies HTML 4.01. An SGML processor is
allowed to use the FPI to retrieve a DTD, and it obviously does in this
case.

[snip]

Mike
Aug 2 '06 #15

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

Similar topics

2
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
6
by: Amir Hardon | last post by:
I'm new to DOM and can't figure out this thing: I'm trying to add a row to a table with a form field in one of it's cells, but if I'm appending the field to a form it gets out of the table. Can...
4
by: mappo | last post by:
There are about a million posts on how to get rid of the extra space _after_ the form end-tag, but I can't find any that solve my problem: extra space _in_ the form tag. I have a table nestled...
4
by: multimatum2 | last post by:
Hello, I need to enable/disable input text forms... But... I need to have the same style (color...) in both modes.. Could you help me ? Thanx a lot A small sample... ...
2
by: skubik | last post by:
I'm curious as to whether it's possible to create a Form object and populate it with form element objects, strictly in Javascript, without the need to apply the form to a document. Essentially,...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
9
by: whisher | last post by:
Hi. I've managed this simple snippet: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Register</title> <meta...
9
by: stevewy | last post by:
I am trying to write a function that will test all the checkboxes in a particular group of a form (it's a questionnaire), see whether more than three of them are ticked, and display a message if...
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.