$(form).getInputs('radio', 'radio_name');
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
> 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. ;)
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
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.
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
You may use this XPath expression: //input[@type="radio"]
A similar question has been asked and answered here before.
Please search before you post.
PointedEars
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>
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.
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>