Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to get only radio buttons from getElementsByTagName method?

Reply
Thread Tools

How to get only radio buttons from getElementsByTagName method?

 
 
yajiv.vijay@gmail.com
Guest
Posts: n/a
 
      02-15-2008
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?
 
Reply With Quote
 
 
 
 
Rauan Maemirov
Guest
Posts: n/a
 
      02-15-2008
If u use Prototype JS, there is

$(form).getInputs('radio', 'radio_name');
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      02-15-2008
(E-Mail Removed) 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);
}

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

--
Bart
 
Reply With Quote
 
rf
Guest
Posts: n/a
 
      02-15-2008

"Rauan Maemirov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.


 
Reply With Quote
 
Rauan Maemirov
Guest
Posts: n/a
 
      02-15-2008
On Feb 15, 5:54 pm, "rf" <(E-Mail Removed)> wrote:
> "Rauan Maemirov" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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.


I mean, u could watch its' code.
 
Reply With Quote
 
Csaba Gabor
Guest
Posts: n/a
 
      02-15-2008
On Feb 15, 12:37 pm, Bart Van der Donck <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
rf
Guest
Posts: n/a
 
      02-15-2008

"Rauan Maemirov" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 15, 5:54 pm, "rf" <(E-Mail Removed)> wrote:
>> "Rauan Maemirov" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>> > 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.

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

--
Richard.


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      02-16-2008
On Feb 16, 12:14 am, Csaba Gabor <(E-Mail Removed)> wrote:
> On Feb 15, 12:37 pm, Bart Van der Donck <(E-Mail Removed)> wrote:
>
> > (E-Mail Removed) 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

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-17-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

http://jibbering.com/faq/


PointedEars
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-17-2008
Bart Van der Donck wrote:
> (E-Mail Removed) 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$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Taking action only when two sets of radio buttons have both beenselected Steve Swift Javascript 7 07-20-2008 06:02 AM
Created Radio Buttons displaying as if they were conventional buttons Dr. Leff Javascript 3 10-15-2007 09:47 PM
Created Radio Buttons displaying as if they were conventional buttons Dr. Leff Javascript 3 10-15-2007 05:18 AM
ASP.NET Reset Radio Button in a group of Radio Buttons Scott D Johnson ASP .Net 1 09-27-2006 10:38 PM



Advertisments