1

I'm trying to create a function that add automatically value of the current element into another selector, something like this:

function calculate(current,selector) {
    var sum = $(selector).val();
        if (!isNaN($(current).value) && $(current).value.length != 0) {
            sum += parseFloat($(current).value);
        }
    $(selector).val(sum.toFixed(2));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="one" onkeyup="calculate(this,'#three')" >
<input type="text" id="two" onkeyup="calculate(this,'#five')" >
<input type="text" id="three">
<input type="text" id="five">

4 Answers 4

1

You're passing to onkeyup already executed function, so basically you're passing undefined as something that will be called. Try with:

<input type="text" id="two" onkeyup="() => calculate(this,'#five')" >
Sign up to request clarification or add additional context in comments.

Comments

0

Change

$(current).value

to

$(current).val() 

The code changed:

function calculate(current,selector) {
var sum = $(selector).val();
    if (!isNaN($(current).val()) && $(current).val().length != 0) {
        sum += parseFloat($(current).val());
    }
$(selector).val(parseFloat(sum).toFixed(3) );
}

Comments

0

Something like that should work :

<!DOCTYPE html>
<html>
<body>

<input type="text" id="one" onkeyup="calculate(this,'#three')" >
<input type="text" id="two" onkeyup="calculate(this,'#five')" >
<input type="text" id="three">
<input type="text" id="five">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

    function calculate(current,selector) {
        var sum = parseFloat($(selector).val());
        var num = parseFloat($(current).val());
        if(!isNaN(num)){
            if(isNaN(sum)){
                $(selector).val(num)
            }
            else {
                $(selector).val(sum+num)
            }
        } 
    }
</script>
</html>
</body>

But I don't recommand you to do that. You see with the onkeyup you are gonna have some weird result with your sum if someone type something higher than 10

Comments

0

Sometimes you were using .value, sometimes you used .val().

Changing all of them to .val() made it work for me.

Plus, I suggest you to use the unary operator + instead of parse…() methods. Just because it's cooler!

Here is the snippet I ended-up with:

function calculate(current, selector) {
  var sum = +$(selector).val();
  if (!isNaN($(current).val()) && $(current).val().length != 0) {
    sum += +$(current).val();
  }
  $(selector).val(sum.toFixed(2));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="one" onkeyup="calculate(this,'#three')">
<input type="text" id="two" onkeyup="calculate(this,'#five')">
<input type="text" id="three">
<input type="text" id="five">

Hope it helps.

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.