Matlab ignoring uihtml autofocus attribute in 2021b

3 views (last 30 days)
We have several Matlab apps that use uihtml components. In 2020b, we used the autofocus HTML attribute to control which app component was selected when the app started (and then controlled the stack order using Reorder as introduced in 2020b). We're transitioning from 2020b to 2021b and a common complaint from our users is that the autofocus attribute appears to be ignored now.
The attached zip file includes an app & html file with a simple example. When the app is opened in 2020b, the username field is selected and the user can enter text immediately. When the app is opened in 2021b, the user has to click the edit field or press the tab key before they're able to enter text.
Is there a workaround for this behavior change or a more elegant way to control an app's focus on startup?

Accepted Answer

Antonio Hortal
Antonio Hortal on 8 Nov 2021
One thing that you could do is to have an HTML event that requests focus on the input element whenever the startup function of the app finishes:
function startupFcn(app)
app.HTML.HTMLSource = 'usernameInput.html';
% Maybe you need a drawnow() here, but I have tried without it and it
% worked
app.HTML.Data.FocusOnInput = true;
end
In your .html file you will just need to add a listener in the setup function:
htmlComponent.addEventListener("DataChanged", function(evt) {
if (evt.Data.FocusOnInput){
document.getElementById("usernameInput").focus();
}
})
I don't have matlab 2021b (only 2021a, and it behaves the same as 2020b), but I have tried running my code without the "autofocus" property in the <input> and it worked. Hopefully it also works in the 2021b! Let me know how it goes :)

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!