Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get only radio buttons from getElementsByTagName method?

1,604 views
Skip to first unread message

yajiv...@gmail.com

unread,
Feb 15, 2008, 6:23:18 AM2/15/08
to
I am just started using getElementsByTagName. I dont know how to
filter radio buttons from all other INPUT fields. Is there any way to
do that without looping through all the INPUT fields?

Rauan Maemirov

unread,
Feb 15, 2008, 6:31:48 AM2/15/08
to
If u use Prototype JS, there is

$(form).getInputs('radio', 'radio_name');

Bart Van der Donck

unread,
Feb 15, 2008, 6:37:58 AM2/15/08
to
yajiv.vi...@gmail.com wrote:

They neeed to be looped anyhow.

for (i=0; i<document.getElementsByTagName('input').length; i++) {
if (document.getElementsByTagName('input')[i].type == 'radio')
alert('This is radio ' + i);
}

Note that radio buttons are a bit special; every group with the same
name belongs to the same object.

--
Bart

rf

unread,
Feb 15, 2008, 6:54:09 AM2/15/08
to

"Rauan Maemirov" <raua...@gmail.com> wrote in message
news:207887ca-c85a-498b...@s12g2000prg.googlegroups.com...

> If u use Prototype JS, there is

No. Don't use over 100K of "library" just to avoid looping through all input
fields and filtering for radio buttons, which is what that "library" does
anyway.

And, what is a u?

--
Richard.


Rauan Maemirov

unread,
Feb 15, 2008, 7:37:48 AM2/15/08
to
On Feb 15, 5:54 pm, "rf" <r...@invalid.com> wrote:
> "Rauan Maemirov" <rauan1...@gmail.com> wrote in message

I mean, u could watch its' code. ;)

Csaba Gabor

unread,
Feb 15, 2008, 9:14:33 AM2/15/08
to
On Feb 15, 12:37 pm, Bart Van der Donck <b...@nijlen.com> wrote:
> yajiv.vi...@gmail.com wrote:
> > I am just started using getElementsByTagName. I dont know how to
> > filter radio buttons from all other INPUT fields. Is there any way to
> > do that without looping through all the INPUT fields?
> They neeed to be looped anyhow.
>
> for (i=0; i<document.getElementsByTagName('input').length; i++) {
> if (document.getElementsByTagName('input')[i].type == 'radio')
> alert('This is radio ' + i);
> }

However, which is the more efficient loop?

for (idx in aDesc=document.body.getElementsByTagName('input'))
if (aDesc[idx].type == 'radio')
alert ('Radio button:\n ' + aDesc[idx].parentNode.innerHTML);

or

for (idx in aDesc=document.body.getElementsByTagName('*'))
if ((elem=aDesc[idx]).tagName=='INPUT' &&
elem.type == 'radio')
alert ('Radio button:\n ' + elem.parentNode.innerHTML);


I would expect the first one to be faster, but I have never
investigated.

Csaba Gabor from Vienna

rf

unread,
Feb 15, 2008, 5:53:16 PM2/15/08
to

"Rauan Maemirov" <raua...@gmail.com> wrote in message
news:a36e7879-3630-430f...@h11g2000prf.googlegroups.com...

Prototype is not a good example of how to do things.

What is a u? If you mean the word "you" then why not just type it?

--
Richard.


RobG

unread,
Feb 16, 2008, 12:33:23 AM2/16/08
to
On Feb 16, 12:14 am, Csaba Gabor <dans...@gmail.com> wrote:
> On Feb 15, 12:37 pm, Bart Van der Donck <b...@nijlen.com> wrote:
>
> > yajiv.vi...@gmail.com wrote:
> > > I am just started using getElementsByTagName. I dont know how to
> > > filter radio buttons from all other INPUT fields. Is there any way to
> > > do that without looping through all the INPUT fields?
> > They neeed to be looped anyhow.
>
> > for (i=0; i<document.getElementsByTagName('input').length; i++) {
> > if (document.getElementsByTagName('input')[i].type == 'radio')
> > alert('This is radio ' + i);
> > }
>
> However, which is the more efficient loop?
>
> for (idx in aDesc=document.body.getElementsByTagName('input'))

There is no guarantee that the index property of the collection is
enumerable, it isn't in Safari or IE. Therefore the above fails in
those browsers at least.


> if (aDesc[idx].type == 'radio')
> alert ('Radio button:\n ' + aDesc[idx].parentNode.innerHTML);
>
> or
>
> for (idx in aDesc=document.body.getElementsByTagName('*'))
> if ((elem=aDesc[idx]).tagName=='INPUT' &&
> elem.type == 'radio')
> alert ('Radio button:\n ' + elem.parentNode.innerHTML);
>
> I would expect the first one to be faster, but I have never
> investigated.

If speed is an issue, it's not difficult to resolve. A method that
loops over all the elements in a document will almost certainly be
slower than one that loops over only a subset of elements, given the
same loop method and that in most documents inputs will never be more
than say 25% of the elements in a page so that is not even worth
testing.

For, while and do loops have different performances in different
browsers so it is common to just use a for loop unless while offers
some real benefits. Do loops tend to be a little tougher on
maintenance but are useful in few cases.

When selecting elements from a document, Xpath can't be ignored.
Testing Xpath, for loop and for..in gives the following over 10,000
radio buttons (times in ms) on my laptop:

Browser for loop Xpath for..in
Safari 82 415 index not enumerable
Firefox 321 26 150
IE 110 not supported index not enumerable
took 32,500ms to return

Which shows that a for loop is reasonably fast and reliable, other
methods should really only be considered if there is some other
criterion.


--
Rob

Thomas 'PointedEars' Lahn

unread,
Feb 17, 2008, 6:05:35 PM2/17/08
to

You may use this XPath expression: //input[@type="radio"]

A similar question has been asked and answered here before.
Please search before you post.

http://jibbering.com/faq/


PointedEars

Thomas 'PointedEars' Lahn

unread,
Feb 17, 2008, 6:06:56 PM2/17/08
to
Bart Van der Donck wrote:
> yajiv.vi...@gmail.com wrote:
>> I am just started using getElementsByTagName. I dont know how to
>> filter radio buttons from all other INPUT fields. Is there any way to
>> do that without looping through all the INPUT fields?
>
> They neeed to be looped anyhow. [...]

No, they don't. However, not using a loop requires another API, for example
DOM Level 3 XPath or MSXML.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300...@news.demon.co.uk>

Dr J R Stockton

unread,
Feb 18, 2008, 11:19:01 AM2/18/08
to
In comp.lang.javascript message <47B8BDBF...@PointedEars.de>, Mon,
18 Feb 2008 00:05:35, Thomas 'PointedEars' Lahn <Point...@web.de>
posted:

Best to ignore such remarks. BigEars has a Deity Complex, and does not
know how to act as a human being..

--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Thomas 'PointedEars' Lahn

unread,
Feb 18, 2008, 5:23:19 PM2/18/08
to
Dr J R Stockton wrote:
> [...] Thomas 'PointedEars' Lahn [...] posted:

>> yajiv...@gmail.com wrote:
>>> I am just started using getElementsByTagName. I dont know how to
>>> filter radio buttons from all other INPUT fields. Is there any way
>>> to do that without looping through all the INPUT fields?
>> You may use this XPath expression: //input[@type="radio"]
>>
>> A similar question has been asked and answered here before.
>> Please search before you post.
>
> Best to ignore such remarks.

It was a helpful answer accompanied by a polite request that is finally even
backed up by at least this newsgroup's own FAQ.

> BigEars has a Deity Complex,

Pot, kettle, black.

> and does not know how to act as a human being..

So you would consider it inhuman behavior to spare your fellow humans the
hassle of answering the same questions again and again. That displays an
interesting view of humankind on your part. Fortunately, many if not most
people are better than you want them to be. If only you could recognize that.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

0 new messages