0

I am creating a form that takes values from a hash and creates three input boxes(having class amount, acNo,debNo) per key name attribute combination of the hash value and a string bed in my case now i am trying to get the sum of the values in input boxes having "bed" in their name and have class "amount".

 $(document).ready(function(){
 alert('document is loaded');
    $("input[name*='bed'][class='amount']").each(function() {//doen't go in this loop

        alert('in the bed'); //no alert
            $(this).keyup(function(){

                calculateSum();
            });
        });

    });

    function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $("input[name*='bed'][class='amount']").each(function() { 
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("input[name='totalBEDamount']").val(sum.toFixed(2));
    }

I am using the above jquery to get sum of values. The problem is that it works fine as fixed layout fixed layout fiddle but when i am creating the document on radio button selection from a hash the code fiddle with error doesn't work? Why???

1

1 Answer 1

3

Use [class='amount'] and use on() (for your dynamically created elements) in place of [class='column'] like,

$(document).ready(function () {
     $(document).on('keyup',"input[name*='bed'][class='amount']", function () {
         var sum = 0;
         //iterate through each textboxes and add the values
         $("input[name*='bed'][class='amount']").each(function () {
             //add only if the value is number
             if (!isNaN(this.value) && this.value.length != 0) {
                 sum += parseFloat(this.value);
             }

         });
         $("input[name='totalBEDamount']").val(sum.toFixed(2));
     });
});

Working Demo

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

1 Comment

thanks a lot man...no need to say it worked. Yeah I made a mistake in class name but the real was that I was not using on() for dynamically created object.

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.