0

Hey guys,

I have the following code...

And what I want it to do but cant get it to do is add up all the price fields and change the sum every time a number is changed or a new line is added... but its not working?

Can anyone see my problem? And can help me fix this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        

<script type="text/javascript" src="./jquery-1.7.min.js"></script>
<script type="text/javascript">
function calculateSum() {
var sum = 0;
$("#price").each(function() {

//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}

});

$("#sum").html('&pound;' + sum.toFixed(2));
}


$(document).ready(function(){       

    $('.del').live('click',function(){
        $(this).parent().parent().remove();
    });

    $('.add').live('click',function(){
        $(this).val('Delete');
        $(this).attr('class','del');
        var appendTxt = "<tr><td><input name='item[]' type='text' size='40' /></td><td><input name='qty[]' type='text' size='5' value='0' /></td><td><input name='price[]' id='price' type='text' size='20' value='0.00' /></td><td><input type='button' class='add' value='Add Row' /></td></tr>";
        $("tr:last").after(appendTxt);  
    });


    $('#price').keyup(function() {
        calculateSum();
    });         

});
</script>
     </head>
    <body>       
        <table id="options-table">                   
            <tr>
                <td>Item</td>
                <td>Qty</td>
                <td>Price</td>
                <td>&nbsp;</td>
            </tr>                  
            <tr>
                <td><input name="item[]" type="text" size="40" /></td>
                <td><input name="qty[]" type="text" value="0" size="5" /></td>
                <td><input name="price[]" id="price" type="text" value="0.00" size="20" /></td>
                <td><input type="button" class="add" value="Add Row" /></td>
            </tr>
        </table>

<br /><br />
<div id="sum">&pound;0.00</div>

    </body>
</html>

Thanks
Dan

Edited by Danny159

2
Contributors
2
Replies
8
Views
5 Years
Discussion Span
Last Post by Danny159
0

One small oversight:

$('#price').keyup(function() {
    calculateSum();
});

Only binds the event once, you should have used live here too:

$('price').live('keyup', function() {
    calculateSum();
});         

In my example I've added class="price" and used .price instead of #price. Using the id is meant to specify a single item. In your case you have more than one, so use class. My entire code looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
    function calculateSum() {
        var sum = 0;
        $(".price").each(function() {
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
        });
        $("#sum").html('&pound;' + sum.toFixed(2));
    }

    $(document).ready(function(){       
        $('.del').live('click',function(){
            $(this).parent().parent().remove();
        });

        $('.add').live('click',function(){
            $(this).val('Delete');
            $(this).attr('class','del');
            var appendTxt = "<tr><td><input name='item[]' type='text' size='40' /></td><td><input name='qty[]' type='text' size='5' value='0' /></td><td><input class='price' name='price[]' id='price' type='text' size='20' value='0.00' /></td><td><input type='button' class='add' value='Add Row' /></td></tr>";
            $("tr:last").after(appendTxt);  
        });

        $('.price').live('keyup', function() {
            calculateSum();
        });         

    });
    </script>
</head>
<body>       
    <table id="options-table">                   
        <tr>
            <td>Item</td>
            <td>Qty</td>
            <td>Price</td>
            <td>&nbsp;</td>
        </tr>                  
        <tr>
            <td><input name="item[]" type="text" size="40" /></td>
            <td><input name="qty[]" type="text" value="0" size="5" /></td>
            <td><input name="price[]" class="price" id="price" type="text" value="0.00" size="20" /></td>
            <td><input type="button" class="add" value="Add Row" /></td>
        </tr>
    </table>
    <br /><br />
    <div id="sum">&pound;0.00</div>
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.