VBA Cycle through check boxes in JavaScript Generated Table

Viewed 71

I'm trying to check all of of the boxes in a JS generated table named tblItems

I have tried to getElementsByTagName("td") but it just loads everything as an HTML obj and I can't use InStr to find anything that differentiates them. This is what I was trying to use to find a value I could use to pick out the check boxes.

Set AllChkBoxes = appIE.document.getElementById("tblItems").getElementsByTagName("td")
    For Each box In AllChkBoxes
        If InStr(UCase(box), "") <> 0 Then
            MsgBox (box)

        Else
            MsgBox (box)
            End If
    Next box
End Sub

This is what the check boxes look like I was trying to cycle through the value but was unable to. there are a bunch of other td tag names but they are just values in the table or hrefs

<td align="center"><input type="checkbox" name="chkToPay" checked="" value="0"></td>

<td align="center"><input type="checkbox" name="chkToPay" checked="" value="1"></td>

<td align="center"><input type="checkbox" name="chkToPay" checked="" value="2"></td>

<td align="center"><input type="checkbox" name="chkToPay" checked="" value="3"></td>

Thanks to anyone that can help.

2 Answers

Might be below code help I am using jquery :

<table id="tableId">
<tr>
<td align="center"><input type="checkbox" name="chkToPay"  value="0"></td>

<td align="center"><input type="checkbox" name="chkToPay"  value="1"></td>

<td align="center"><input type="checkbox" name="chkToPay"  value="2"></td>

<td align="center"><input type="checkbox" name="chkToPay" value="3"></td>
</tr>


$(document).on("click", '#tableId tbody :checkbox[name="chkToPay"]', function(event) {
var currentRows = $('#tableId');

$.each(currentRows, function() {
        $(this).find(':checkbox[name=chkToPay]').each(function() {
            if($(this). prop("checked") == true){
                var parentTr = $(this).parents("tr");
                $(this).prop('checked', true);
        alert($(this).val());
        // if you need text from another td you can access by below line
                //alert(parentTr.children("td").eq(0).text());

            }
        });
    });
        });

Would help to see more html. Looks like perhaps you can use an attribute = value selector for example. In the following I target the name attribute and associated value

Dim list As Object, i As Long
Set list = ie.document.querySelectorAll("[name=chkToPay]")
For i = 0 To list.Length - 1
    Debug.Print list.item(i).Value
Next

You can also combine with a parent id if exists

Dim list As Object, i As Long
Set list =  ie.document.querySelectorAll("#tableId [name=chkToPay]")
For i = 0 To list.Length - 1
    Debug.Print list.item(i).Value
Next

You can also combine with another attribute to enhance specificity.

Dim list As Object, i As Long
Set list =  ie.document.querySelectorAll("#tableId [type=checkbox][name=chkToPay]")
For i = 0 To list.Length - 1
    Debug.Print list.item(i).Value
Next

You get the idea.

If you want to check a specific one add in the value attribute

ie.document.querySelector("#tableId [name=chkToPay][value='1']").Click
Related