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 = () => {
     frm.page.wrapper.find(".a1").on("click", function(evt){ 
         frappe.web_form.set_df_property("a1", "options", "<img src=LINK>"); 
         refresh_field("a1"); 
         frm.set_value("r1", 1); 
         refresh_field("r1");
      });
};

Thanks!