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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Buttons and Length check 2

Status
Not open for further replies.

teblack

Programmer
Apr 30, 2004
45
I have a page that builds a list of employees with a radio button attached to each row. I have written a vbscript routine to spin through the list to make sure that at least one row has been checked. I'm trying to get the length value of the radio button called "selected", so that I know how many occurences there are in the list. But I'm having problems with the len command, and not sure how to get it fixed.

Code:
	Sub checksbox_onclick
        Dim x        
      
        For x = 0 to len(UserSelect_Form.selected.value)
            If UserSelect_Form.elements(x).checked = True Then
      			MsgBox document.UserSelect_Form.elements(x).value
            End If
        Next 
    End Sub


If I replace the "len(....)" with a value of like 6, then it works just fine, but I won't know the number of rows for each displayed page because they can be different. Any help or thoughts would be greatly appreciated.

Thanks in advance for any help with this......

TBlack -
 
Maybe something like this ?
For Each x In Document.UserSelect_Form.elements
If x.checked = True Then
MsgBox x.Value
End If
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Is there a way to just get the radio buttons off of the page. I have Dropdown boxes, checkboxes, and textboxes on my pages. I just want to go through the radio buttons. I am using a variable to name them so I can just use the name. Any help is appreciated
 
When you assign several HTML elements the same [tt]id[/tt] value they get placed into a collection in IE. IE collections have a [tt]length[/tt] property. Note that index values in IE collections are 0-based, so the [tt]length[/tt] will be 1 greater than the index of the last element in the collection.

When pages have dynamic content (i.e. one way or another you generate a list of elements with the same id) this can be a pain. In cases where only one ID'ed element exists, [tt]idval(0)[/tt] will result in an error exception because there will be an [tt]idval[/tt] element, but not an [tt]idval[/tt] collection. One hack is to always create an extra initial [tt]<input type=hidden id=idval>[/tt] element. This will be your "zero element" and you can code to ignore it.

When one uses the [tt]name[/tt] property (which is really only supposed to be used for form elements) IE will try to set the [tt]id[/tt] to the same value as the [tt]name[/tt]. This is not reliable though! It is a "best effort" attempt and may result in an unexpected [tt]id[/tt] value.

If you are going to script against the IE DHTML object model (as opposed to the W3C DOM) it is always safest to define both [tt]name[/tt] and [tt]id[/tt], generally with the same values.


The upshot of this is that you could have a form named/id'ed "myForm" with a radio button set where every radio element in the set/group is named/id'ed "myRadio" and use syntax like:

[tt]For x = 0 To myForm.myRadio.length - 1
MsgBox myForm.myRadio(x).value
Next[/tt]

Note that [tt]length[/tt] is just as valid for other collections, such as [tt]elements[/tt].

You can also use:

[tt]For Each x In myForm.myRadio
MsgBox x.value
Next[/tt]
 
Thanks! I have kind of figured it out so far. I will never know how many groups of radio buttons I will have on a particular page and I will never know the names of the radio buttons, I wish I could hard code names and id's but that just isn't possible for this application.

I do know that each grouping of radio buttons will have a different name since the name is populated by the database. So I use a Select case on the element type, when radio comes up I grab the name, I check to see if any of them are checked if so I exit, if not I will keep looping through.

If nothing is checked and an element with type radio with a different name appears then I know the user forgot to select something so I throw up a flag and display a pop up.

thanks for the information.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top