Update on keyup value of a text field

4.2k views Asked by At

I have the following code :

{% for product in app.session.get('aBasket') %}
    <input id="product_quantity_{{ product['product_id'] }}" class="form-control quantity" type="text" value="{{ product['product_quantity'] }}" name="" onkeyup="calculatePrice({{ product['product_price'] }},{{ product['product_id'] }})">
    <span id="total_price_{{ product['product_id'] }}">{{ product['product_quantity'] * product['product_price'] }}</span>
{%endfor}
<div id="total_price_basket">TOTAL:0</div>

And my js function :

<script type="application/javascript">
    function calculatePrice(price, product_id) {
        var x = document.getElementById("product_quantity_"+product_id);
        var total_price = price * x.value;
        $("#total_price_"+product_id).html(total_price);
        $("#total_price_basket").html();
    }
</script>

So, the price for all products it works fine, the question is how to change the value for div>total_price_basket onkeyup? I must gather the amount of each product. Can you help me please? Thx in advance

5

There are 5 answers

3
dfsq On BEST ANSWER

I would make use of jQuery and get rid of obtrusive inline event handlers. For this HTML markup will change a little to use data attributes:

{% for product in app.session.get('aBasket') %}
    <input data-id="{{ product['product_id'] }}" 
           data-price="{{ product['product_price'] }}"
           value="{{ product['product_quantity'] }}"
           class="form-control quantity" type="text">
    <span class="total" id="total_price_{{ product['product_id'] }}">{{ product['product_quantity'] * product['product_price'] }}</span>
{%endfor}
<div id="total_price_basket">TOTAL:0</div>

JS:

$('.quantity').on('keyup', function () {

    // Update individual price
    var price = $(this).data('price') * this.value;
    $('#total_price_' + $(this).data('id')).text(price);

    // Update total
    var total = 0;
    $('.total').each(function() {
        total += Number($(this).text());
    });
    $('#total_price_basket').text('TOTAL: ' + total);
})
// Trigger initial calculation if needed
.trigger('keyup');

Demo: http://jsfiddle.net/c64xfrpr/

1
bjaksic On

Initially, when rendering a page, set total_price_basket to a value from the server. In your javascript set a global variable to store sum of all products:

var sum = 0;

Then in your calculatePrice method append these two lines:

sum += total_price;
$("#total_price_basket").html('TOTAL: ' + sum);

Also do the subtracting when the product is removed.

<script type="application/javascript">
    var sum = 0;

    function calculatePrice(price, product_id) {
        var x = document.getElementById("product_quantity_"+product_id);
        var total_price = price * x.value;
        $("#total_price_"+product_id).html(total_price);
        sum += total_price;
        $("#total_price_basket").html('TOTAL: ' + sum);
    }
    </script>
0
Tushar On

Use parseFloat to convert string to float:

The parseFloat() function parses a string argument and returns a floating point number.

See comments inline:

var total_price = 0; // Set default to zero
// ^^^^^^^^^^^^^^^^^
function calculatePrice(price, product_id) {
    var x = document.getElementById("product_quantity_" + product_id);
    var total_price = parseFloat(price) * parseFloat(x.value);
    //                ^^^^^^^^^^          ^^^^^^^^^^
    $("#total_price_" + product_id).html(total_price);
    $("#total_price_basket").html();
}
0
Vikash On

You can try this :

$(function(){
var price=100;
$('input.quantity').keyup(function(){

var all=$('input.quantity');
var total=0;
for(var i=0;i<all.length;i++){
   if(!isNaN(all[i].value)){
        total+=price*all[i].value
    }   
}
$("#total_price_"+product_id).html('TOTAL : '+total);
});   

});
0
Akhil On

try with this

var x = document.getElementById("product_quantity_"+product_id);
        var total_price = price * x.value;
        console.log(x.value);