MDL-85774 login: Check for username/password fields before moving focus

When the login form is not shown but $CFG->loginautofocus is turned on,
we need to verify first that the username and password fields exist
before we move the focus. Otherwise, the JS will break.
This commit is contained in:
Jun Pataleta
2025-10-23 11:28:48 +08:00
parent 4c66cdde04
commit 52ec42bb01
+4 -3
View File
@@ -219,10 +219,11 @@
require(['core_form/events'], function(FormEvent) {
function autoFocus() {
const userNameField = document.getElementById('username');
if (userNameField.value.length == 0) {
const passwordField = document.getElementById('password');
if (userNameField && userNameField.value.length == 0) {
userNameField.focus();
} else {
document.getElementById('password').focus();
} else if (passwordField) {
passwordField.focus();
}
}
autoFocus();