How to get only radio buttons from getElementsByTagName method?

Discussion in 'Javascript' started by yajiv.vijay@gmail.com, Feb 15, 2008.

  1. Guest

    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?
     
    , Feb 15, 2008
    #1
    1. Advertising

  2. If u use Prototype JS, there is

    $(form).getInputs('radio', 'radio_name');
     
    Rauan Maemirov, Feb 15, 2008
    #2
    1. Advertising

  3. 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').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
     
    Bart Van der Donck, Feb 15, 2008
    #3
  4. rf Guest

    "Rauan Maemirov" <> wrote in message
    news:...

    > 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.
     
    rf, Feb 15, 2008
    #4
  5. On Feb 15, 5:54 pm, "rf" <> wrote:
    > "Rauan Maemirov" <> wrote in message
    >
    > news:...
    >
    > > 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. ;)
     
    Rauan Maemirov, Feb 15, 2008
    #5
  6. Csaba Gabor Guest

    getElementsByTagName efficiency

    On Feb 15, 12:37 pm, Bart Van der Donck <> wrote:
    > 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').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
     
    Csaba Gabor, Feb 15, 2008
    #6
  7. rf Guest

    "Rauan Maemirov" <> wrote in message
    news:...
    > On Feb 15, 5:54 pm, "rf" <> wrote:
    >> "Rauan Maemirov" <> wrote in message
    >>
    >> news:...
    >>
    >> > 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.
     
    rf, Feb 15, 2008
    #7
  8. RobG Guest

    Re: getElementsByTagName efficiency

    On Feb 16, 12:14 am, Csaba Gabor <> wrote:
    > On Feb 15, 12:37 pm, Bart Van der Donck <> wrote:
    >
    > > 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').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
     
    RobG, Feb 16, 2008
    #8
  9. 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
     
    Thomas 'PointedEars' Lahn, Feb 17, 2008
    #9
  10. Bart Van der Donck wrote:
    > 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$>
     
    Thomas 'PointedEars' Lahn, Feb 17, 2008
    #10
  11. In comp.lang.javascript message <>, Mon,
    18 Feb 2008 00:05:35, Thomas 'PointedEars' Lahn <>
    posted:
    > 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. 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.
     
    Dr J R Stockton, Feb 18, 2008
    #11
  12. Dr J R Stockton wrote:
    > [...] Thomas 'PointedEars' Lahn [...] posted:
    >> 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>
     
    Thomas 'PointedEars' Lahn, Feb 18, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott D Johnson
    Replies:
    1
    Views:
    1,078
    Scott D Johnson
    Sep 27, 2006
  2. Eskimo
    Replies:
    0
    Views:
    153
    Eskimo
    Feb 7, 2005
  3. Dr. Leff
    Replies:
    3
    Views:
    299
    David Mark
    Oct 15, 2007
  4. Dr. Leff
    Replies:
    3
    Views:
    231
    David Mark
    Oct 15, 2007
  5. Steve Swift
    Replies:
    7
    Views:
    113
    Steve Swift
    Jul 20, 2008
Loading...

Share This Page