finding <input> item in Mozilla

J

jodleren

Hi

I tried getElementById, it works in IE, but how I find items on a form
properly?
I can write a small for myself, but is there a function to find
<input> items on a form?
That is, standard JS which also works in Mozilla

WBR
Sonnich
 
J

jhurstus

Hi

I tried getElementById, it works in IE, but how I find items on a form
properly?
I can write a small for myself, but is there a function to find
<input> items on a form?
That is, standard JS which also works in Mozilla

WBR
Sonnich

getElementById should works on all modern browsers, including
Mozilla. If your code is working in IE only, it's probably due to the
fact that you set the name attribute but not the id attribute of the
input tag. IE falls back to the name attribute when using
getElementById, but Mozilla based browsers do not.

Make sure your tag looks like this: <input type="text" name="some_id"
id="some_id" ... />

Also, duplicate Id's within a page can cause getElementById to behave
in strange ways.

-Joey
 
J

jodleren

getElementById should works on all modern browsers, including
Mozilla.  If your code is working in IE only, it's probably due to the
fact that you set the name attribute but not the id attribute of the
input tag.  IE falls back to the name attribute when using
getElementById, but Mozilla based browsers do not.

Make sure your tag looks like this: <input type="text" name="some_id"
id="some_id" ... />

Will try. I found this to work:

if(document.forms[0].elements['currentuser'])
alert(11);

Is that correct JS?
 
J

jhurstus

getElementById should works on all modern browsers, including
Mozilla.  If your code is working in IE only, it's probably due to the
fact that you set the name attribute but not the id attribute of the
input tag.  IE falls back to the name attribute when using
getElementById, but Mozilla based browsers do not.
Make sure your tag looks like this: <input type="text" name="some_id"
id="some_id" ... />

Will try. I found this to work:

if(document.forms[0].elements['currentuser'])
  alert(11);

Is that correct JS?

That's another way to do it, but getElementById is a little cleaner as
it doesn't depend on the form being the first on the page.
 
A

Anthony Levensalor

Hi
I tried getElementById, it works in IE, but how I find items on a form
properly?
I can write a small for myself, but is there a function to find
<input> items on a form?
That is, standard JS which also works in Mozilla
WBR
Sonnich
getElementById should works on all modern browsers, including
Mozilla. If your code is working in IE only, it's probably due to the
fact that you set the name attribute but not the id attribute of the
input tag. IE falls back to the name attribute when using
getElementById, but Mozilla based browsers do not.
Make sure your tag looks like this: <input type="text" name="some_id"
id="some_id" ... />
Will try. I found this to work:

if(document.forms[0].elements['currentuser'])
alert(11);

Is that correct JS?

That's another way to do it, but getElementById is a little cleaner as
it doesn't depend on the form being the first on the page.

GEBID is not a cleaner solution, you are mistaken. getElementById is
/easier/, NOT cleaner. GEBID has to do a lookup on all the elements in
the document collection, while the forms collection is by necessity a
smaller one.

The way the OP ended up doing it is better than using the getElementById
crutch.

To the OP:

name your form with a name attribute, and you can access any input
element with:
document.forms['formName'].elements['elementName']
 
A

Anthony Levensalor

getElementById should works on all modern browsers, including
Mozilla. If your code is working in IE only, it's probably due to the
fact that you set the name attribute but not the id attribute of the
input tag. IE falls back to the name attribute when using
getElementById, but Mozilla based browsers do not.

Make sure your tag looks like this: <input type="text" name="some_id"
id="some_id" ... />

Will try. I found this to work:

if(document.forms[0].elements['currentuser'])
alert(11);

Is that correct JS?

Yes, and that will work on any browser that implements the ECMA
specification anywhere even approaching properly.

Notes:
- semicolon is not required to end a statement.
- You should name the form, and use the named index instead of 0 for
your own edification, unless it's the first one on the page and you want
to leave it numeric.
 
D

David Mark

getElementById should works on all modern browsers, including
Mozilla.  If your code is working in IE only, it's probably due to the
fact that you set the name attribute but not the id attribute of the
input tag.  IE falls back to the name attribute when using
getElementById, but Mozilla based browsers do not.

Make sure your tag looks like this: <input type="text" name="some_id"
id="some_id" ... />

The tag should only look like that if it is an XHTML document.
 
T

Thomas 'PointedEars' Lahn

jodleren said:
I tried getElementById, it works in IE, but how I find items on a form
properly?

Using the form object's `elements` collection property.
I can write a small for myself, but is there a function to find
<input> items on a form?
That is, standard JS which also works in Mozilla

The `elements' collection is available for named form controls since DOM
Level 0, and is specified for objects that implement the HTMLFormElement
interface of W3C DOM Level 2 HTML (there *also* for controls with an ID),
so that works in Mozilla and any other scriptable user agent, in contrast
to document.getElementById() which is not available without an
implementation of W3C DOM Level 1+ Core.


PointedEars
 
T

Thomas 'PointedEars' Lahn

And as it would appear that you also don't know it already, all of this has
little to do with the programming language(s). You are scripting an API
here that is not part of JavaScript anymore since version 1.4 (before
Gecko), and has never been part of Microsoft JScript.


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,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top