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

getElementByName

P: n/a
is it document.getElementByName or document.getElementsByName?
^

reason I ask is that I have to change ref's in my code from document.all
to sthg that works also in FF... so changed something like this

docslide = document.all['slide'];
docslide = document.getElementByName("slide");

but then looked in Danny G's DOM ref
(http://www.dannyg.com/dl/JSB5RefBooklet.pdf)

and there's a "s" after "Element"

if I search in google for document.getElementsByName with or w/o that
"s" I find entries for both.. pls, which one is it? b/c neither one is
working for me.. thanks..

Frances

Jan 9 '06 #1
Share this Question
Share on Google+
20 Replies


P: n/a
On 09/01/2006 15:39, Frances wrote:
is it document.getElementByName or document.getElementsByName?
The latter. The method returns a collection so its name uses a plural form.

[snip]
but then looked in Danny G's DOM ref
Based on the testament of other regulars in this group, Danny Goodman is
not a trusted source of scripting information. I've only read one
article written by him (an e-mail), and its content would have me draw
the same conclusion.

[snip]
[N]either one is working for me.. thanks..


Link?

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 9 '06 #2

P: n/a


Frances wrote:
is it document.getElementByName or document.getElementsByName?
The specification is here
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259>
the specified method name is getElementsByName.
reason I ask is that I have to change ref's in my code from document.all
to sthg that works also in FF... so changed something like this

docslide = document.all['slide'];


If you have e.g.
<div id="slide">
with a unique id then you might be better off to use
document.getElementById('slide')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 9 '06 #3

P: n/a
Frances wrote:
is it document.getElementByName or document.getElementsByName?
The latter one, where the name is because the method returns a collection
of element object references, not one element object reference.
^ reason I ask is that I have to change ref's in my code from document.all
to sthg that works also in FF... so changed something like this

docslide = document.all['slide'];
docslide = document.getElementByName("slide");
Use IDs and document.getElementById(...) instead. Unless you do not want
to support IE 4 users anymore, you should not replace document.all[...] with
the latter but implement it as an alternative if the feature-test for the
latter failed.

<URL:http://pointedears.de/scripts/test/whatami>
<URL:http://jibbering.com/faq/#FAQ4_26>
but then looked in Danny G's DOM ref
(http://www.dannyg.com/dl/JSB5RefBooklet.pdf)
That is not a good book, as there are few good books about the subjects
discussed here out there, if any.

<URL:http://jibbering.com/faq/#FAQ3_1>

As for me, Flanagan's book is old enough and revealed too many errors
here, that I cannot recommend it either.
and there's a "s" after "Element"

if I search in google for document.getElementsByName with or w/o that
"s" I find entries for both.
Yes, there are many incompetent people out there which is why the number
of Google hits does not qualify as proof for existence of a name.
pls, which one is it? b/c
"w/o" == "without"?
"b/c" == "because"?

Do not assume that other people understand these abbreviations.
neither one is working for me.. [...]


<URL:http://jibbering.com/faq/#FAQ4_43>
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259>
<URL:http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-getElBId>
PointedEars
Jan 9 '06 #4

P: n/a

Frances wrote:
is it document.getElementByName or document.getElementsByName?
As mentioned by many others, it is document.getElementsByName.
docslide = document.getElementByName("slide");


It doesn't work as you expected because this method looks for HTML
elements with the name 'slide', not elements with an attribute value of
'slide'. Thus, you should use a different method, for example:

docslide = document.getElementById("slide");

where you have:

<div id = "slide">...</div>

Jan 9 '06 #5

P: n/a
VK

Frances wrote:
is it document.getElementByName or document.getElementsByName?


That was well explained to others but there is always something to add
:-)

getElementsByName in IE (Internet Explorer) grabs all elements *with
such id or with such name* - it doesn't give a damn of difference. It
is not standard but fair spelled in the method description on MSDN.

Also it is W3C (and common sense) requirement that id's and names must
be unique for each element on one page. So the presence of
getElementsByName method by itself is a mistery. I think that someone
in W3C was just starting to learn HTML while writing the relevant
standards for it (it happens).

Do not confuse it with getElementsByTagName - here "elements" are
plural because on the same page can be any amount of <p>, <div> and so
elements..

Jan 9 '06 #6

P: n/a
On 09/01/2006 20:02, VK wrote:

[snip]
Also it is W3C (and common sense) requirement that id's and names must
be unique for each element on one page.


An id attribute value must be unique within a document, however, this is
not true for all name attributes.

The name attributes that are, on the whole, backwards-compatible
identifiers (A, APPLET, FORM, FRAME, IFRAME, IMG, MAP) should be unique.
Form controls (BUTTON, INPUT, OBJECT, SELECT, TEXTAREA) can, and in some
cases (radio buttons and checkboxes) must, be duplicates. The remaining
elements (META, PARAM) are not so well-defined as their use tends to
fall outside the scope of HTML.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 9 '06 #7

P: n/a
VK wrote:
Frances wrote:
is it document.getElementByName or document.getElementsByName?

That was well explained to others but there is always something to add
:-)

getElementsByName in IE (Internet Explorer) grabs all elements *with
such id or with such name* - it doesn't give a damn of difference. It
is not standard but fair spelled in the method description on MSDN.

Also it is W3C (and common sense) requirement that id's and names must
be unique for each element on one page.


That is a requirement of IDs but not for all elements that can have a name
- e.g. radio buttons.
[...]
I think ...


.... you didn't check the specification:

<URL:http://www.w3.org/TR/html4/index/attributes.html>
[...]

--
Rob
Jan 9 '06 #8

P: n/a
Michael Winter wrote:
On 09/01/2006 20:02, VK wrote:
Also it is W3C (and common sense) requirement that id's and names must
be unique for each element on one page.
An id attribute value must be unique within a document, however, this is
not true for all name attributes.


True.
The name attributes that are, on the whole, backwards-compatible
identifiers (A, APPLET, FORM, FRAME, IFRAME, IMG, MAP) should be unique.


How did you get that idea?
PointedEars
Jan 9 '06 #9

P: n/a
CK
Definitely getElementsByName... It returns an array of elements with that
name attribute. Then you can access individual values by looping through
the array.

aFields = document.getElementsByName('test');
for (x = 0; aFields.length; x++)
{ alert(aFields[x].value); }

hope this helps.

Chris

"Frances" <fd***@yahoo.com> wrote in message
news:43**********@x-privat.org...
is it document.getElementByName or document.getElementsByName?
^

reason I ask is that I have to change ref's in my code from document.all
to sthg that works also in FF... so changed something like this

docslide = document.all['slide'];
docslide = document.getElementByName("slide");

but then looked in Danny G's DOM ref
(http://www.dannyg.com/dl/JSB5RefBooklet.pdf)

and there's a "s" after "Element"

if I search in google for document.getElementsByName with or w/o that "s"
I find entries for both.. pls, which one is it? b/c neither one is
working for me.. thanks..

Frances

Jan 9 '06 #10

P: n/a
On 09/01/2006 21:13, Thomas 'PointedEars' Lahn wrote:
Michael Winter wrote:


[snip]
The name attributes that are, on the whole, backwards-compatible
identifiers (A, APPLET, FORM, FRAME, IFRAME, IMG, MAP) should be unique.


How did you get that idea?


I recall a similar discussion last year.

Subject: firefox and innerHTML
Message-ID: <ew******************@text.news.blueyonder.co.uk >
Date: 2005-07-12 16:49:14 GMT

Though browsers can cope with duplicated attribute values, it is my
opinion that the /intent/ was for them to be unique.

As I mention in the cited post, the addition of these name attribute was
a revision to HTML 4.0 (which introduced the id attribute). Netscape,
which first designed and implemented several of the listed elements
before HTML 4.0, and did not support the id attribute, presumably
adopted the name attribute as an identifier due to the precedent set by
links (that is, A elements).

As I also note in that post, the collection nature of DOM 0 does not
extend to FORM elements. In a quick test in Fx 1.5 (because it was
running), it doesn't cover APPLET, A (obviously), IMG, or IFRAME
elements, either (I didn't test FRAME and MAP elements). If duplication
was expected, why would scripting mechanisms not have been written to cope?

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 9 '06 #11

P: n/a
VK

Lee wrote:
Where did you get the idea that names must be unique?
Have you never used checkboxes?


You meant to say "radio button" of course. Yes, radio button *group* is
one, and what's going to be two?
It is nice from W3C to provide a whole separate method for a single
case instance, but should it be added then to form or radio object
itself instead?

The only other occasion I may think of (with my humble mind of course)
is that ugly "improvement" of PHP where elements named like "name[]"
will be automatically glued together into array on the server side. I
call this ugly because one should never break long time commonly
accepted standards to implement your own freshly invented ones.
Plus it gives a terrible school to newcomers. If you look at this group
archives like 2-3 years ago it will be full of questions like: how to
access form element named "foo[]" - document.forms[0].foo[] doesn't
work! and similar... Plus a reasonnable question after such "practice"
is: why the hell id's should be any different? - thanks to PHP.
Thinking back document.getElementsByName may be a W3C "endorsment"
towards PHP (? pure speculation).

Jan 9 '06 #12

P: n/a
VK wrote:
You meant to say "radio button" of course. Yes, radio button *group*
is one, and what's going to be two?
It is nice from W3C to provide a whole separate method for a single
case instance, but should it be added then to form or radio object
itself instead?
I often use forms with multiple <input> tags with the same name.
Radio buttons are not a special case, it's just a common example of the
general case.
Multiple inputs with the same name (even of different types!) has been
supported forever.
The only other occasion I may think of (with my humble mind of course)
is that ugly "improvement" of PHP where elements named like "name[]"
will be automatically glued together into array on the server side. I
call this ugly because one should never break long time commonly
accepted standards to implement your own freshly invented ones.
Which standard is broken? None.
Plus it gives a terrible school to newcomers. If you look at this
group archives like 2-3 years ago it will be full of questions like:
how to access form element named "foo[]" - document.forms[0].foo[]
doesn't work! and similar...


Which is why I always recommend accessing form elements with the syntax:

document.forms['formname'].elements['elementname']

This will cause no problems with form fields with '[]' in them.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 9 '06 #13

P: n/a
VK said the following on 1/9/2006 5:32 PM:

<snip>
I call this ugly because one should never break long time commonly
accepted standards to implement your own freshly invented ones.


There are terms for breaking long time standards to implement your own:

progress
innovation

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 10 '06 #14

P: n/a
On Mon, 09 Jan 2006 17:22:52 +0100, in comp.lang.javascript , Thomas
'PointedEars' Lahn <Po*********@web.de> in
<20****************@PointedEars.de> wrote:
Frances wrote:

[snip]
if I search in google for document.getElementsByName with or w/o that
"s" I find entries for both.


Yes, there are many incompetent people out there which is why the number
of Google hits does not qualify as proof for existence of a name.


Which makes me wonder what is the verifiably objectively wrong
statement with the largest number of Google hits?
--
Matt Silberstein

Do something today about the Darfur Genocide

http://www.beawitness.org
http://www.darfurgenocide.org
http://www.savedarfur.org

"Darfur: A Genocide We can Stop"
Jan 19 '06 #15

P: n/a
Matt Silberstein wrote on 19 jan 2006 in comp.lang.javascript:
Which makes me wonder what is the verifiably objectively wrong
statement with the largest number of Google hits?


"True"

<http://www.google.com/search?q=true>

777,000,000 hits. [some of which are true, methinks]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 19 '06 #16

P: n/a
Evertjan. said the following on 1/19/2006 11:47 AM:
Matt Silberstein wrote on 19 jan 2006 in comp.lang.javascript:
Which makes me wonder what is the verifiably objectively wrong
statement with the largest number of Google hits?
"True"


"Not" :)
<http://www.google.com/search?q=true>

777,000,000 hits. [some of which are true, methinks]


<URL: http://www.google.com/search?q=not >
10,470,000,000 hits.

<URL: http://www.google.com/search?q=true >
Shows me 1,190,000,000 hits.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 19 '06 #17

P: n/a
Randy Webb wrote on 19 jan 2006 in comp.lang.javascript:
Evertjan. said the following on 1/19/2006 11:47 AM:
Matt Silberstein wrote on 19 jan 2006 in comp.lang.javascript:
Which makes me wonder what is the verifiably objectively wrong
statement with the largest number of Google hits?


"True"


"Not" :)
<http://www.google.com/search?q=true>

777,000,000 hits. [some of which are true, methinks]


<URL: http://www.google.com/search?q=not >
10,470,000,000 hits.

<URL: http://www.google.com/search?q=true >
Shows me 1,190,000,000 hits.


However Not is an unary operator and not a statement in itself, it needs
true or false for completion.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 19 '06 #18

P: n/a
Evertjan. said the following on 1/19/2006 6:28 PM:
Randy Webb wrote on 19 jan 2006 in comp.lang.javascript:
Evertjan. said the following on 1/19/2006 11:47 AM:
Matt Silberstein wrote on 19 jan 2006 in comp.lang.javascript:
Which makes me wonder what is the verifiably objectively wrong
statement with the largest number of Google hits?
"True"

"Not" :)
<http://www.google.com/search?q=true>

777,000,000 hits. [some of which are true, methinks]

<URL: http://www.google.com/search?q=not >
10,470,000,000 hits.

<URL: http://www.google.com/search?q=true >
Shows me 1,190,000,000 hits.


However Not is an unary operator and not a statement in itself, it needs
true or false for completion.


Why?

http://www.google.com/search?q=why
2,760,000,000 hits

But you are right about Not.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 20 '06 #19

P: n/a
Randy Webb wrote on 20 jan 2006 in comp.lang.javascript:
Why?

http://www.google.com/search?q=why
2,760,000,000 hits

But you are right about Not.


A negative expression, Randy.

Or was it ment to be:

"But you are right about Knot" ?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #20

P: n/a
Evertjan. said the following on 1/20/2006 9:08 AM:
Randy Webb wrote on 20 jan 2006 in comp.lang.javascript:
Why?

http://www.google.com/search?q=why
2,760,000,000 hits

But you are right about Not.

A negative expression, Randy.


It was? Wow.
Or was it ment to be:

"But you are right about Knot" ?


But you are right about "Not".

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 20 '06 #21

This discussion thread is closed

Replies have been disabled for this discussion.