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

Using variables with checkbox name 2

Status
Not open for further replies.

AgentM

MIS
Jun 6, 2001
387
US
How can I use a variable with check box name?
eg. document.all.checkbox&i.checked

where i= variable name

so I can use it in a loop

Thank you

 
Execute "document.all." & i & ".checked = True"

or

Execute "my_check = document.all." & i & ".checked"
If my_check = False Then ...
 
Well, you COULD do something like that, but what you really want is something like an array of checkboxes.

In DHTML we have the next best thing, a collection that we can use in a similar fashion. Just name the elements with the same name as in this little toy (copy/paste into an HTML file and enjoy):
Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=VBScript>
Option Explicit

Function StrIIf(blnCond, strTrue, strFalse)
  If blnCond Then
    StrIIf = strTrue
  Else
    StrIIf = strFalse
  End If
End Function

Sub cmdShow_onClick()
  Dim strResults
  Dim i
  For i = 0 To 2
    strResults = strResults & _
      &quot;<P>Option &quot; & CStr(i + 1) & &quot;: &quot; & _
      StrIIF(document.all.chkTest(i).checked, _
        &quot;Checked!&quot;, &quot;Unchecked.&quot;) & &quot;</P>&quot;
  Next
  document.all.celResults.innerHTML = strResults
End Sub
</SCRIPT>
</HEAD>
<BODY>
<TABLE>
	<TR>
		<TD width=&quot;150&quot;>
			<P><INPUT id=chkTest type=checkbox name=chkTest>Option 1</P>
			<P><INPUT id=chkTest type=checkbox name=chkTest>Option 2</P>
			<P><INPUT id=chkTest type=checkbox name=chkTest>Option 3</P>
      <P><INPUT id=cmdShow type=button value=&quot;Show Me&quot; name=cmdShow></P>
    </TD>
    <TD id=celResults width=&quot;150&quot; valign=top>
    </TD>
  </TR>
</TABLE>
</BODY>
</HTML>
Note that items in a collection are indexed from 0, not 1!

Sorry about the code-wrapping.
 
sfvb your solution didn't work for me
mycheck has the value 'document.all.1.value' when i = 1 . But I cannot check it for true or false.

dilettante's code worked just fine.
Thank you both of you.

 
dilettante, thanks.

That's the kind of code example I was looking for when I had a similiar situtation, but couldn't find any.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top