Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Button length

Status
Not open for further replies.

robi2

Programmer
Jan 19, 2003
62
IL
Hey, how can i get the length of a radion group?

Assume i have these buttons:

<input type=&quot;radio&quot; name&quot;a&quot;>
<input type=&quot;radio&quot; name&quot;a&quot;>
<input type=&quot;radio&quot; name&quot;a&quot;>
<input type=&quot;radio&quot; name&quot;a&quot;>

And i want to get the length, the number of radios in this radio group, i would do:
window.document.myform.myradio.length

But if i was to access it this way:

window.document.myform.elements[0].length

it will return Undefined.

Suggestions?

Thanks.
 
in your example, document.myform.elements[0] refers to the very first radio element button. they exist as a group only when you reference them by name.
=========================================================
if (!succeed) try();
-jeff
 
So what your saying is that there is no way to find the length the second way?
 
you could enumerate the elements of the form, checking the &quot;name&quot; property. count the number of times you encounter the name you're looking for.

a more generic way would be to enumerate the form elements. keep an array of &quot;FormElement&quot; objects...a &quot;FormElement&quot; object would store two properties: name and count. if a FormElement object exists with the name of the current element, increment its count property, else create a new FormElement by that name with a count of 1.
=========================================================
if (!succeed) try();
-jeff
 
Actually i have a better way, well u said it not me.
I just use the name to group them, and still do it in a loop.

var mypath = window.document.myform.elements

mypath[mypath.name].length

And that it.

Thanks alot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top