0

I have 8 different text fields in my form, it's a part of customer bill. Here it is

<input type="text" name="txtcustomduty" class="form-control" placeholder="Customs Duty">
<input type="text"  name="txtlcltranspotation" class="form-control" placeholder="Local Transportation">
......

up to 8

From this I want to show the sum of all the values as total value

<span>Total extra cost:1678</span>

It should be changed when the values of any text field is changed, so how can I do it perfectly using keyup event?

UPDATE

I have attached an onkeyup event to each textfield

`onkeyup="findSum(this.value)"'

and i am using a global array for store the input values var extras=[]

function findSum(value)
{
    if(value!=''){
        console.log(value);
        extras.push(parseInt(value));
        if(extras!='')
        $('#extratotal').text(extras.reduce(getSum));
        else $('#extratotal').text('0');
    }
}

But its not worked well

1
  • 2
    Great question, but what have you tried so far? Commented May 17, 2016 at 9:37

3 Answers 3

2

You can get SUM of all inputs that have form-control class on keyup event like this:

$('input.form-control').on('keyup',function() {   
    var total = 0;
    $('input.form-control').each(function(){
        if (this.value == ''){
            total += parseInt(0);
        }else{
            total += parseInt(this.value);
        }
    });
    $('#total').val(total);
});
input {
   display: block;   
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="txtcustomduty" class="form-control" placeholder="Customs Duty" >
<input type="text"  name="txtlcltranspotation" class="form-control" placeholder="Local Transportation" >
<input type="text"  name="other" class="form-control" placeholder="other" >

Total extra cost: <input id="total" >

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

5 Comments

Worked partially but getting NAn
@BlessanKurien Very odd, it works perfectly for me, have you filled all inputs with valid values?
Yes i filled up with numeric
Its worked dude:) actually its my mistake ,i've typo in my class name
@BlessanKurien that's nice, glad to help you :)
2

You can use the target.value property of the event passed to the key listener - this will give you the value of the input field:

document.addEventListener('input', 'keyup', function(e) {
    // use e.target.value here
}

Just add this to a running total and update the text inside the listener function.

Comments

1

I have defined in JavaScript instead of jQuery. Try it..

<script>
function sum()
{

    var sum = 0;
    var array_field = document.getElementsByClassName('sum_field');
    for(var i=0; i<array_field.length; i++)
    {
         var value = Number(array_field[i].value);
         if (!isNaN(value)) sum += value;
    }
    document.getElementById("total").innerHTML = sum;
 }
</script>
<body>
    <input type="text" name="txtcustomduty" class="form-control sum_field" placeholder="Customs Duty" onkeyup="sum()">
    <input type="text"  name="txtlcltranspotation" class="form-control sum_field" placeholder="Local Transportation" onkeyup="sum()">
    <p>Total:<span id="total">0</span></p>
</body>

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.