jagmitg
jagmitg

Reputation: 4546

Onfocus and off-focus textbox

I am written a jquery script that automatically detects on the number that a user has entered. If the user has entered 2,4,6,8,10 - they will automatically have a confirm box popping up stating that there is an offer.

If they click YES, they get redirected. However, if they click Cancel, it focuses back on the textbox and the confirm box appears again.

Is there anyway i can make it so if a user clicks on CANCEL, the focus is off the textbox so no loop of confirm box occurs.

HTML

<input type="text" class="Quantity" id="Quantity9438851" name="Quantity" value="1" style="width:30px">

JQUERY

$("#Quantity9438851").bind("change paste keyup", function() {
    if($(this).val() == 2){
     var didConfirm = confirm("There is currently an offer for 2 polo shirt for £25. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://blahblah.com/&Quantity=10";
      }
    }else if($(this).val() == 4){
     var didConfirm = confirm("There is currently an offer for 4 polo shirt for £50. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }        
    }else if($(this).val() == 6){
     var didConfirm = confirm("There is currently an offer for 6 polo shirt for £75. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }                
    }else if($(this).val() == 8){
     var didConfirm = confirm("There is currently an offer for 8 polo shirt for £100. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }                
    }else if($(this).val() == 10){
     var didConfirm = confirm("There is currently an offer for 10 polo shirt for £100. Click YES to recieve discount.");
      if (didConfirm == true) {
        window.location.href = "http://google.com/Registration/blah";
      }                
    }
});

http://jsfiddle.net/NUJrL/3/

Upvotes: 0

Views: 262

Answers (1)

Johan
Johan

Reputation: 35194

Why not simply use the blur event?

$("#Quantity9438851").bind("blur", function() {

    var val = $(this).val();

    if(!(val % 2) && val <= 10){

      var conf = confirm("There is currently an offer for "
         + val +" polo shirt for £25. Click YES to recieve discount.");

      if (conf) 
        window.location.href = "http://google.com/&Quantity=10";


    }
});

http://jsfiddle.net/NUJrL/6/

Upvotes: 1

Related Questions