Explore the community Forums Lectora Lectora Questions & Answers focus() not working Reply To: focus() not working

#427275 Score: 0
Profile photo of Joe Wieloch
Joe Wieloch
Moderator
beginner
intermediate
friend finder
contributor
LUC16 Attendee
verified member
wise owl
LUC17 Attendee
advanced
picture perfect
73 pts
@wheels

This is the way I recommend setting the focus to a button:

Use an action on the target button (Skip to Main Content):

Skip To Main Content
– Action: On Show -> Run JavaScript -> (content below)

%HTMLNAME%.div.getElementsByTagName('button')[0].focus();

This creates a portable (you can copy it to other titles without worrying about what the object’s name or id is) action that sets the focus to the object it is attached to.

————–

If you need to set the focus when another action happens, like when the user clicks a button, then you need to do a little more work:

On the button you want to set the focus to assign a known name:
Target Button (where you are setting the focus)
– Action: On Show -> Run JavaScript -> (content below)

getDisplayWindow().myTargetButton = %HTMLNAME%;

Trigger Button (triggers the set focus)
– Action: On Mouse Click -> Run JavaScript -> (content below)

getDisplayWindow().myTargetButton.div.getElementsByTagName('button')[0].focus();

Assigning the HTMLNAME to a known name makes this a portable solution that can be reused in other titles.

Hope this helps!

  • This reply was modified 1 year, 3 months ago by Profile photo of Joe Wieloch Joe Wieloch.
  • This reply was modified 1 year, 3 months ago by Profile photo of Joe Wieloch Joe Wieloch.
Attachments: