0

The problem is that when i change the option it keeps summing, and i would like for it to only sum one time the value of 10.00, and subtract only once if the value is AR .

LIVE LINK: http://jsfiddle.net/A3qLG/1/

JQUERY:

$(document).ready( function() {

    $('#country').on('keyup change', function(e){

        var subt_value = $('#subt0').text();

        if($(this).val() == 'AR' || $(this).val() == ''){           
        if(subt_value != '0.00' ){$('#subt0').text(parseFloat(subt_value-10).toFixed(2));}      
        }else{          
        var add_sub = parseFloat(subt_value)+parseFloat('10.00');
        $('#subt0').text(parseFloat(add_sub).toFixed(2));
        }

    });

});

HTML:

<label id="subt0">8.90</label>

<select id="country">
  <option value="US">USA</option>
  <option value="BR">Brasil</option>
  <option value="AR">Argentina</option>
  <option value="CU">Cuba</option>
</select> 

5 Answers 5

1

I would change the HTML to this to store the original value:

<label id="subt0" data-original="8.90">8.90</label>

And then adjust the Javascript to this:

$(document).ready( function() {

    $('#country').on('keyup change', function(e){

        //Changed the following line to get the original value      
        var subt_value = $('#subt0').attr('data-original');

        //Changed this to say, NOT AR and has a choice per the comments.            
        if($(this).val() != 'AR' && $(this).val().length > 0) {
          var add_sub = parseFloat(subt_value)+parseFloat('10.00');
          $('#subt0').text(parseFloat(add_sub).toFixed(2));
        } else {
          //Otherwise put it back to the original value
          $('#subt0').text(subt_value);
        }

    });

});
Sign up to request clarification or add additional context in comments.

5 Comments

here is ur code online jsfiddle.net/A3qLG/4 but there is a problem when i go back to the Argentina option i get -1.10
That's because 8.9 - 10 = -1.10. You wanted it to subtract once, right? If not, then you'll need to give some details about how your code should display results.
i made it subtract because country Argentina does not have a recharge all other country's must add $10 from the original price...so lets say the total order is $8.90 if i select any other country besides Argentina it should add $10 to the total... any thoughts of what can i do to get that?
Yep, just convert what you typed into code. I have edited that section in my answer.
Just made a correction by adding an else clause to restore the value in case it was changed.
1

try this code

$(document).ready( function() {

    $('#country').on('keypress change', function(e){

        var subt_value = $('#subt0').text();

        if($(this).val() == 'AR' || $(this).val() == ''){           
        if(subt_value != '0.00' ){$('#subt0').text(parseFloat(subt_value-10).toFixed(2));}      
        }else{          
        var add_sub = parseFloat(subt_value)+parseFloat('10.00');
        $('#subt0').text(parseFloat(add_sub).toFixed(2));
        }

    });

});

1 Comment

When submitting an answer, code by itself may solve the problem, but it doesn't give an answer. Please explain your changes.
1

You need to remember previous #country select value, and use it when making decision:

$(document).ready(function () {
    var prevValue = '';
    $('#country').on('keyup change', function (e) {
        var newValue = $(this).val();
        var subt_value = parseFloat($('#subt0').text());

        if ((newValue == 'AR' || newValue == '')) {
            if (prevValue != '' && prevValue != 'AR') {
                $('#subt0').text((subt_value - 10).toFixed(2));
            }
        } else {
            if (prevValue == 'AR' || prevValue == '') {
                $('#subt0').text((subt_value + 10).toFixed(2));
            }
        }
        prevValue = newValue;
    });

});

http://jsfiddle.net/A3qLG/5/

Comments

0

Simply remove the onkeyup and onchange event handler functions after they have done their work.

$('#country').off('keyup change');

2 Comments

This was my first impression, too, but from the post saying to subtract only once... or add only once... I take it to be asking "each time the value is changed".
than instead of using an anonymous function in the handler, declare it as a named function.Wait a sec, I'll edit my post
0

Jquery provide a .change() specifically for <select>

You want to sum only one time and substract only one time base on the condition of the selected value AR. Therefore you only need to check the selected value if there is change and do the math within the if-else statement. Finally attach the result with .toFixed(2)

var subt_value = $('#subt0').text(); 
var add_sub = 0, f=10.00;
$('#country').change(function(){
    if ($(this).val() != 'AR'){
       add_sub = parseFloat(subt_value)+f;
    }else{
        add_sub = parseFloat(subt_value)-f;
    }

    $('#subt0').text(add_sub.toFixed(2));

});

JSFiddle Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.