Setting Focus In HTMLRenderer

General APL language issues
Post Reply
paulmansour
Posts: 431
Joined: Fri Oct 03, 2008 4:14 pm

Setting Focus In HTMLRenderer

Post by paulmansour »

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: 431
Joined: Fri Oct 03, 2008 4:14 pm

Re: Setting Focus In HTMLRenderer

Post by paulmansour »

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.
Post Reply