.focus()
in jQuery will focus on the element, but won't give access to the text input when its within a .blur()
event. You can use the .setTimeout()
function to call the focus so it happens just after the .blur()
event:
$("#Amount").blur(function() {
if ($("#Amount").val() < 10000) {
alert('The minimum amount is $10,000.');
setTimeout(function() {
$("#Amount").focus();
}, 100);
}
});
However, I would suggest changing things a little more to check the use has actually input a number as well:
$("#Amount").blur(function() {
var obj = $(this);
if (isNaN(obj.val()) || obj.val() < 10000) {
$('#result').html('The minimum amount is $10,000.');
setTimeout(function() {
obj.focus();
}, 100);
}
});
See it in action here
*Corrected as per Levi Morrison's comment*