Reputation: 5660
I need to keep the focus on the text input when the user moves to the next input without putting something in the previous one. In short, if it is null (left blank) or not valid, how do I keep the focus on that input until the conditions are satisfied?
Upvotes: 0
Views: 1706
Reputation: 84140
$("#inputID").blur(function(){
if ("#inputID").val() == "")
{
$("#inputID").focus();
}
});
Code done this way for the concept of explaining that is always referring to the same input. In practice 'this' would be a better option.
Upvotes: 1
Reputation: 163228
$( element ).blur( function( e ) {
var ev = e || event;
if( $( this ).val() == '' ) {
$( this ).focus();
return false;
}
});
Upvotes: 1
Reputation: 3099
You don't really need jquery for it
<input type="text" onblur="if (!this.value) this.focus()">
Although, I don't think that it's a good idea. If it's a form, then it's better to show the list of required fields when the user tries to submit it.
Upvotes: 0