0

I'm using the following code to execute a value update based on a code entered. But it returns wrong.

Ex: If the promo code is to reduce the price by 10, and the current total is 100, this returns 80. which should be 90.

jQuery(document).ready(function() {
$('#promo-code').keyup(function() {
  if ($("input:text[name='promo-code']").val() == "intro") {
    var promoval = parseFloat($("input:text[name='total']").val());
    $("#total").val(promoval-10);  
  }
});
JudeAinsly
  • 73
  • 2
  • 12

3 Answers3

0

Try some else instead of keyup like focusout as below. Because when you are using keyup it gets fire every keyup event.

$(document).ready(function() {
  $('#promo-code').focusout(function() {
    if ($("input:text[name='promo-code']").val() == "intro") {
      var promoval = parseFloat($("input:text[name='total']").val());
      $("#total").val(promoval-10);  
    }
  });
});

Check below snippet for reference.

$('#total').val(100);
$('#promo-code').focusout(function() {
  if ($(this).val() == "intro") {
    var promoval = parseFloat($('#total').val());
    $("#total").val(promoval - 10);
  } else {
    alert('invalid promo');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="promo-code">
<input type="text" id="total">
RaJesh RiJo
  • 4,302
  • 4
  • 25
  • 46
  • 1
    Thank you RaJesh. But the reason I was using `keyup` is to update the total field without a click. Also, even though I use this method, if I re-enter the same code and focusout, the total keeps reducing each and every time. – JudeAinsly Sep 07 '17 at 07:04
0

An improvised solution for your problem:

jQuery(document).ready(function() {

    $('#promo-code').change(function() {

        if ($(this).val() == "intro") {

           var promoval = parseFloat($("input:text[name='total']").val());
           $("#total").val(promoval-10);

        }
    }
});
Deepansh Sachdeva
  • 1,168
  • 9
  • 16
0

I hope you set the html correctly, and the keyup functionality should work just fine. But you would also like to check other methods as mentioned in other answers here. I would suggest that you go for change method.

jQuery(document).ready(function() {
$('#promo-code').keyup(function() {
  if ($("input:text[name='promo-code']").val() == "intro") {
    var promoval = parseFloat($("input:text[name='total']").val());
    $("#total").val(promoval-10);  
  }
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="promo-code" type="text" id="promo-code"/>
<input name="total" type="text" value="100" id="total"/>
Kiran Dash
  • 4,816
  • 12
  • 53
  • 84
  • Thank you, yes the HTML is correct. 'change' does the job. But I was trying to update the price real time. :/ any other suggestion? – JudeAinsly Sep 07 '17 at 06:44
  • What do you mean by trying to update the price in real time? From the code you have provided, it is not clear which is the price in real time. Please make your question a bit more clear – Kiran Dash Sep 07 '17 at 06:48