views:

134

answers:

1

I have a bunch of checkboxes where a user can select from a list of systems which they want to load. The checkboxes are created like so:

'***************************************************************
' Print systems from DB in a table for the load page.
'***************************************************************
Function PrintSystemTable()

    Set con = CreateObject("ADODB.Connection")

    con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & systemsDBPath

    Set systemRs = CreateObject("ADODB.Recordset")
    systemRs.CursorLocation = adUseClient

    Set sensorRs = CreateObject("ADODB.Recordset")
    sensorRs.CursorLocation = adUseClient

    document.write("<table class=satelliteList>")
        document.write("<tr class=""tableHeader"">")
            document.write("<td>Select</td>")
            document.write("<td width=""100"">System Name</td>")
            document.write("<td width=""100"">Description</td>")
            document.write("<td width=""75"">Files</td>")
        document.write("</tr>")

    systemRs.Open "SELECT * FROM System ORDER BY SystemName", _
        con, adOpenStatic, adLockOptimistic

    do until systemRs.eof
        document.write("<tr valign=""top"">")
            document.write("<td>")
            document.write("<INPUT TYPE=CHECKBOX NAME=""system"" VALUE=""" & systemRs("SystemName") & """>")
            document.write("</td>")
            document.write("<td>")
            document.write(systemRs("SystemName"))
            document.write("</td>")
            document.write("<td>")
            document.write(systemRs("Description"))
            document.write("</td>")
            document.write("<td>")
            document.write(guiPath & systemRs("Satellite"))
            sensorRs.Open "SELECT * FROM SystemSensors WHERE System='" & systemRs("SystemName") & "'", _
                con, adOpenStatic, adLockOptimistic
            do until sensorRs.eof
                document.write("<br>" & guiPath & sensorRs("Sensor"))
                sensorRs.movenext
            loop
            sensorRs.Close
            document.write("</td>")
            'adoDBRecordset("FieldName") & "<br>")
        document.write("</tr>")
        systemRs.movenext
    loop

    document.write("</table>")

    con.Close

End Function

They are not in a form. I'm trying to access my checkboxes by their name (system) and iterate through them to see which is checked.

'find the systems that are selected
'access an array of all the checkboxes?
Dim checkboxes
Set checkboxes = document.getElementsByName("system")

'For i=0 to UBound(checkboxes)
For Each chk in checkboxes
'   If (checkboxes(i).checked = true) Then
    If (chk.checked = true) Then
        document.write("A checkbox is checked <br>")
    Else
        document.write("Unchecked <br>")
    End If
  'document.write(chk.value & "<br />")
Next

I've tried iterating through using both a for and for each loop. I get the same error either way. Here is what happens:

Right now I have two test samples in my database, so I get two things I can select. If I select the first item and push my button that runs my checker script, it prints "A checkbox is checked" and I get an error (Permission denied) on my page saying that I can't access the checked property. It does NOT print "Unchecked". If I select my SECOND item, leaving the first item unchecked, it prints "Unchecked" and doesn't print "A checkbox is checked".

If I select both items it prints "A checkbox is checked" ONCE and gives the same error.

If I don't select either of the items it prints "Unchecked" ONCE (instead of once per unchecked item) and I get the same permission denied error telling me I can't access the checked property.

Before I started testing on the checked value, I tried printing the value of each checkbox, but attempting to acccess the value property also gave me permission denied.

The strange thing is it seems like it's "working" for the first element (since it will print an appropriate message for one element only), even though it still throws a permission denied error. This leads me to believe that I'm either not using document.getElementsByName() properly (does it return an array?), or I'm not iterating properly... or some other mystery that's beyond me.

This is CLIENT-SIDE vbscript, NOT ASP. Javascript is also not an option; I am using client-side vbscript with HTML for a specific reason. Don't trouble yourself with why I'm using it and tell me it's stupid because I agree; I don't have a choice.

How can I access and iterate through my checkboxes of the same name (system), and see which are checked and get their value?

+1  A: 

Any document.write() after the page has loaded will replace the entire page, that's why the script only finds the first checkbox but not the others.

I have modified your code as follows, and everything works fine now:

<SCRIPT LANGUAGE="VBScript"> 
<!--
Dim checkboxes 
Set checkboxes = document.getElementsByName("system") 

Set e = document.createElement("p")   'ADDED
document.body.appendChild e           'ADDED

' For i=0 to UBound(checkboxes) 
For Each chk in checkboxes 
'   If (checkboxes(i).checked = true) Then 
    If (chk.checked = true) Then 
        e.innerHtml = e.InnerHtml + "A checkbox is checked <br>"  'MODIFIED
    Else 
        e.innerHtml = e.InnerHtml + "Unchecked <br>"   'MODIFIED
    End If 
    'document.write(chk.value & "<br />")    
Next 
</SCRIPT>

I hope this answers your question.

fmunkert
Awesome! Thanks! I didn't even think about document.write interrupting the rest of my code...
Lauren