467,119 Members | 1,187 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,119 developers. It's quick & easy.

Can't select form using getElementById

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?

--
Thanks,

Justin.
http://www.opera.com/mail/
Jul 23 '05 #1
  • viewed: 5378
Share:
3 Replies
Justin Sane wrote:
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.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Jul 23 '05 #2
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>

Jul 23 '05 #3
Justin Sane wrote:
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, me@privacy.net 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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Matt Ratliff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.