Keep your focus on Inputtext

Asked

Viewed 488 times

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 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.

2 answers

5


I did this example with jQuery, it works so you have two input one mandatory, and another desable, then you can’t click on input that is disabled.

When you fill in the required one it removes the desabled of the other input in the keyup.

inserir a descrição da imagem aqui

OBS: if you want to style the input:disabled just use the pseudo class :disabled. Notice that I removed the tabindex of the first input when it is already valid, even so with keyboard it becomes inaccessible. In the script when you click on the document it focuses on the first input, but if that input been :valid if you click on the document it will focus on the other input, also in CSS I stylized the input:valid to not have mouse event.

$(document).on('click', function () {
	$('#nome').focus();
	if ($('#nome:valid')) {
		$('#pass').focus();
	}
});

$('#nome').keyup(function () {
	if ($(this).val()) {
		$(this).attr("tabindex","-1").siblings('input').prop('disabled', false);
	}
});
input:valid {
	pointer-events: none;
	user-select: none;
}
:disabled {
	background:azure;
	border-color:red;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<input autofocus type="text" id="nome" required autocomplete="off">
<input type="text" id="pass" disabled required>

  • your solution worked perfectly, but my idea is to keep the focus on the Input that has the focus. If the focus is on Input tt, even if I click out of the field, the focus should stay on the same Input. Same for the Register field. It has how to do?

  • Face this will make you in qq place you clone in the document focus on the TT, but as you will remove this later I’ll owe you, until you leave here and I should not touch the pc hj... $(document).on('click', function(){&#xA; $('#tt').focus();&#xA;});

  • @Seixas has a detail on his idea, if he can only focus on the second input, like he will be able to edit the first input if he cannot click it, and how he will be able to click on Ubmit?

  • Hugo, 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. A similar idea to a collector. I always start from the basics, I tried to stay focused on the field and I couldn’t. The navigation between the fields will be done based on the result of the call ajax. Would you have any hints? Thank you for your reply

  • @Seixas gave a reformed script and CSS, I think now is more in line with what you asked

  • Show Hugo, it worked. Thanks!!!

  • @Cool seixas that worked as you wanted, just keep in mind the usability of this, try to do a test with users to see if this behavior is really fitting... [] s

Show 2 more comments

2

Hello, it looks like you’re using jQuery, right? Try to focus the input on any screen click by using the function click.

$(document).ready(function(){
    $(document).on("click", function(){
        var campoLogin = $("#login");
        var campoSenha = $("#password");

        // Verifica se campo login está focado e se o clique não foi no campo de senha
        if(campoLogin.is(":focus") && $(this).attr("id") != "password"){
            campoLogin.focus();
        }else{
            campoSenha.focus();
        }
    });
});



I hope I helped, hug!

  • Lucas, thank you for your return, but it didn’t work. I understood your solution, but if the focus is in the login field, you need to remain in the login, if you are in the password, you should remain in the password field.

  • Hello, try again with these updates. I hope it meets what you want to do! Hug.

  • 1

    Lucas , his solution was show. Thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.