To me, this sounds like HTMX-style programming. That bypasses much of what Anvil has to offer, but if it’s familiar to you, that may be more important.
Not my area of expertise, but since you’re working at the HTML/JavaScript level, I’d start here, to get familiar with the provided tools and techniques: Using JavaScript. You can build up from there.
Give the href an anvil-name or id, then use the html custom component logic to add a click event. Just have a play around. Failing that give the link an ID and use the JS bridge.
You may have good reasons for creating links programmatically using HTML, but in Anvil it’s usually much easier to add a Link component and setup its events programmatically and work in Python only, rather than working like those poor people in the outer non-Anvil world with HTML and Javascript.