From 52ec42bb012ec6924ea68334ccc313ac4252a764 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 23 Oct 2025 11:26:44 +0800 Subject: [PATCH] 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. --- lib/templates/loginform.mustache | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/templates/loginform.mustache b/lib/templates/loginform.mustache index 5b204b1c9a3..577e181f51a 100644 --- a/lib/templates/loginform.mustache +++ b/lib/templates/loginform.mustache @@ -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();