How to use custom script in web form

Hi! This script works great in the document but fails when used in the web form. Console logs “frm is not defined” (the error is marked on the frm in the second line, before “.page.wrapper…”). How should I modify it in order to adapt to a web form? I’ve tried using DOM by referencing it by id and using an onclick event but didn’t work.

frappe.web_form.after_load = () => {".a1").on("click", function(evt){ 
         frappe.web_form.set_df_property("a1", "options", "<img src=LINK>"); 
         frm.set_value("r1", 1);