2

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
  • I tried building a solution for you but couldn't get IE to actually run VBScript for me :( basically you need to access the box.innerHTML or box.text to get the actual text content from the element. Commented Jan 16, 2019 at 19:41
  • Thanks for the attempt, I'll give it a shot. Commented Jan 16, 2019 at 19:50

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Dude this is perfect! Thanks for the help.
1

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());

            }
        });
    });
        });

1 Comment

I'll try a couple other things first. If I can't get it working do you know how to execute the jquery from VBA or VBS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.