1

I want to check some condition on blur of input box in jQuery. Once I lose focus from Input box I am not able to get focus again to that box.

Code:

if ($("#Amount").val() < 10000) { 
    alert('The minimum amount is $10,000.'); 
    $("#Amount").focus();
} 
else {
}

Once i lose focus from $("#Amount") it's not setting focus to that control.

Thanks in advance.

Scoobler
  • 9,696
  • 4
  • 36
  • 51
Anvesh
  • 309
  • 1
  • 8
  • 24

4 Answers4

2

Well, this is what you are looking for, I believe: http://jsfiddle.net/6Seks/5/

Edit: just noticed how close my answer is to Scroobler's post, though mine doesn't suffer the larger than 10000 bug his does.

Community
  • 1
  • 1
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
1

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

Community
  • 1
  • 1
Scoobler
  • 9,696
  • 4
  • 36
  • 51
  • 1
    @Scroobler This won't work mate... put in a number greater than 10000 and it still doesn't work. See my code (http://jsfiddle.net/6Seks/5/) and try if you can see what you did wrong ;) – Levi Morrison Mar 31 '11 at 08:04
  • LoL - nice spot on the mistake! I'll blame the coffee not kicking in yet :-) – Scoobler Mar 31 '11 at 08:17
0

I´m not sure if i understand what you are looking for.
If you what do disable an input box after doing something you can use:

jquery

$("#Amount").prop("disabled", true);

javascript

document.getElementById("Amount").disabled = true;

so it would be something like:

$("#Amount").change(function() { 
    if (this.value > 10000) {
        alert('The minimum amount is $10,000.');
    }
    else {
        document.getElementById("Amount").disabled = true;
    };
})


...sorry if this isn't what you are looking for.

trovin
  • 9
  • 2
0

try some thing like this:

$("#Amount").blur(function() {
    if ($(this).val() < 10000) {
        alert('your message...');
        $(this).focus();
    }
})
Flavio CF Oliveira
  • 5,235
  • 13
  • 40
  • 63