Setting Focus In HTMLRenderer

General APL language issues

Setting Focus In HTMLRenderer

Postby paulmansour on Wed Dec 27, 2023 5:43 am

I'm trying to set the focus to a particular element dynamically using something like:

      Focus←{
_←'hrH'⎕WC'HTMLRenderer'
h←'<input type=text id="input1">'
h,←'<input type="text" id=input2">'
hrH.HTML←h
_←hrH.ExecuteJavaScript'document.getElementById("input2").focus();'
0
}


Which is not working for me.

It does work to use the `autofocus` attribute, but that will only work on page load:

      Focus2←{
_←'hrH'⎕WC'HTMLRenderer'
h←'<input type=text id="input1">'
h,←'<input type="text" id=input2" autofocus="">'
hrH.HTML←h
0
}


But in a single page app I may be replacing content and then need to dynamically set the focus, so this won't do.

Any ideas? I tried putting it on a Dyalog form first, but to no avail. I'm hoping I'm just missing something obvious and we don't need a new method for the HTMLRenderer...
paulmansour
 
Posts: 430
Joined: Fri Oct 03, 2008 4:14 pm

Re: Setting Focus In HTMLRenderer

Postby paulmansour on Tue Jan 02, 2024 10:48 pm

Ok, the above example was bad HTML (missing a double quote), but that's not the problem. The browser needs a moment to do whatever it does. So this works:

      FocusExample1←{
_←'hrH'⎕WC'HTMLRenderer'
h←'<input type=text id="input1">'
h,←'<input type="text" id="input2">'
hrH.HTML←h
_←⎕DL 0.1
_←hrH.ExecuteJavaScript'document.getElementById("input2").focus()'
0
}


Problem solved, I think.
paulmansour
 
Posts: 430
Joined: Fri Oct 03, 2008 4:14 pm


Return to Language

Who is online

Users browsing this forum: Morten|Dyalog and 1 guest