Applying checkbox to the highlighted invoices in Cash Receipts Entry via a custom button

Applying checkbox to the highlighted invoices in Cash Receipts Entry via a custom button.
The below logic works for just one row, but not all of the highlighted rows. It works for the row selected, but not the other rows highlighted but not selected.