2
I have a form and need to focus on an input again until it satisfies the required, example:
$('#text1').blur(function() {
var teste = $('#text1').val().length;
console.log(teste);
if(teste > 5){
$('#text1').focus();
}else{
$('#text2').focus();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
Input 1:
<input type="text" name="text1" id="text1">
</label>
<br><br>
<label>
Input 2:
<input type="text" name="text2" id="text2">
</label>
It is possible to proceed in this way?
What would be the problem?
– Paulo Roberto Rosa
Good morning Paulo, the problem was with the page load, because this form comes from json and as it was using an external js with some functions the same did not work accordingly and Focus() as well as other functions of jquery did not work, I copied the js functions below the form and it worked. I’ll try to figure out why to put a response.
– WMomesso
maybe you were trying to execute the code of Focus before including jQuery in your page, you must be sure that jQuery is being included before (above) than the codes, otherwise it won’t work at all. and I also suggest that the code be within a
$(document).ready(function(){ });
This ensures that the event will only be assigned to the element after it is already created. And if it is created with Ajax, you can run the codes in the method.done()
ajax running when ajax ends.– Paulo Roberto Rosa