Can't select form using getElementById

J

Justin Sane

I haven't been able to select a form using getElementById. I only can
select a form using the "name" property of the <form> tag. Is there
another way to select a form, or is it even possible to use getElementById
to select a particular form?
 
L

Lee

Justin Sane said:
I haven't been able to select a form using getElementById. I only can
select a form using the "name" property of the <form> tag. Is there
another way to select a form, or is it even possible to use getElementById
to select a particular form?

Yes, it's possible.
Does your form have an ID attribute? What browser(s)?
Show a small test case that fails.
The following works in IE and Firefox:

<html>
<body>
<form name="alpha" id="alpha">
<input name="beta" value="gamma">
</form>
<script type="text/javascript">
var a=document.getElementById("alpha");
alert(a.beta.value);
</script>
</body>
</html>
 
D

David Dorward

Justin said:
I haven't been able to select a form using getElementById. I only can
select a form using the "name" property of the <form> tag. Is there
another way to select a form, or is it even possible to use getElementById
to select a particular form?

Using document.getElementById('formId') should work, but
document.forms['formId'] is probably better supported.

The majority of things people want to do with forms are usually easier to do
with "this" in a form element event handler or "this.form" in an form
control event handler.
 
T

Thomas 'PointedEars' Lahn

Justin said:
I haven't been able to select a form using getElementById. I only
can select a form using the "name" property of the <form> tag.

Probably you mean the `name' attribute of the `form' element.
Markup elements, which consist of (a) tag(s) and sometimes
non-empty content, have attributes; objects have properties.
Is there another way to select a form, or is it even possible to
use getElementById to select a particular form?

1. name != ID. If you use names, use

document.getElementsByName("element_name")[zero_based_index]

2. Use the both standards compliant and downwards compatible

document.forms["form_name"]

reference or any shortcut of it (`this.form' suffices in
an event handler attribute value of a form control that
is decendant of a `form' element.)

3. This question was not new. Please read previous discussions before
posting.

4. Despite what the provider tells you, (e-mail address removed) is not an
(existing) e-mail address and so using it in the From or Reply-To
header is a violation of Internet protocols as well as a disregard
of (Usenet) Netiquette. Stop using it and start maintaining your
inbox.


PointedEars
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top