How to get only radio buttons from getElementsByTagName method?

Y

yajiv.vijay

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?
 
B

Bart Van der Donck

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').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.
 
R

rf

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?
 
R

Rauan Maemirov

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?

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

Csaba Gabor

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').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
 
R

rf

Rauan Maemirov said:
I mean, u could watch its' code. ;)

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?
 
R

RobG

They neeed to be looped anyhow.
for (i=0; i<document.getElementsByTagName('input').length; i++) {
if (document.getElementsByTagName('input').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.
 
T

Thomas 'PointedEars' Lahn

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.

http://jibbering.com/faq/


PointedEars
 
T

Thomas 'PointedEars' Lahn

Bart said:
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
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Mon,
18 Feb 2008 00:05:35 said:
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. BigEars has a Deity Complex, and does not
know how to act as a human being..
 
T

Thomas 'PointedEars' Lahn

Dr said:
[...] Thomas 'PointedEars' Lahn [...] posted:
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
 

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
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top