3
I have a form with two Inputtext, I need the focus always remain in the Input and only pass to the following via script, in case the user clicks outside the Input, the focus needs to remain in the Input. I’m trying to set the focus, but I’m not getting it. I even tried the Firefox console (document.getElementById("login").focus();
), but I get the Undefined message. In the following code, the expected result would be to keep the focus on Input by clicking anywhere on the screen, but only Alert is being displayed. Any tips? Thanks for the help.
HTML
login:<br><s:textfield name="login" id="login" required="true" cssStyle="width:287px; height:20px;" onblur="this.style.backgroundColor='#F7F5E9'" onfocus="this.style.backgroundColor='#FFFFFF'"/>
Script
$(document).ready(function() {
var txtLogin = document.getElementById("login");
txtLogin.addEventListener('blur', event => {
event.preventDefault();
alert('Passei aqui!!!')
txtLogin.focus();
});
});
If I understand correctly, you can use onChange instead of onBlur, including preventDefault() and/or stopImmediatePropagation(), but I believe there is a great risk of you causing an infinite loop for the user. It would not be better to assign error/highlighting classes to the field and not submit the form if the value is not corrected?
– GustavoAdolfo
@Gustavoadolfo the idea is to read a code in the first field, make an ajax call, if successful, the focus goes to the second field that will read codes and accumulate via ajax as well. An idea similar to a collector. I always start from the basics, I tried to stay focused on the field and I couldn’t. Would you have any hints? Thank you for your reply.
– Seixas