how to fix the focus on an input and select the content of this

0

This question may be absurd but it does not work codes that I found in search

What I have is the following:

$(".nota").focusout(function(){
    var tx = $(this).val();
    if(tx.length == 2){
        alert("El valor ingresado no debe terminar en un punto");
        $(this).focus();
        $(this).select();
    }
});

The alert works well, but the input does not change.

    
asked by Sagara 05.01.2018 в 17:21
source

1 answer

2

Actually your code does work, what you should do is change the alert by another way of displaying the message, besides that the alert is no longer used, it generates problems with your code because when the% is executed if shows the alert , the input recovers its focus and selects its content but when you give click in the button accept you would be leaving again the input which would repeat the whole process generating a alert that is never going to close.

$(".nota").focusout(function(event){
    var tx = $(this);
    if(tx.val().length == 2){
        $("#mensaje").html("El valor ingresado no debe terminar en un punto");
        setTimeout(function() {
          $(tx).focus();
          $(tx).select();
        }, 10)
    }else{
        $("#mensaje").html("");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" class="nota">

<div id="mensaje"></div>
    
answered by 05.01.2018 / 17:34
source