0

I Want update the total of all the products in the cart page when quantity is updated. what I tried is

$d=$table.each(function(){
        return Number($(this).find('.cart_price>p>span').html())*Number($(this).find('.cart_quantity_input').val());
    }).toArray();
    alert($d.length);

but seems its only taking the value from the first product only I tried .map() yet same result. I even tried total+= instead of return (total=0 initialized at first) yet its returning value only from first <tr>.

<tr>'s of my HTLM inside <table> looks like:

<tr>
    <td class="cart_product">
        ...
    </td>
    <td class="cart_description">
        ...
    </td>
    <td class="cart_price">
        <p>₹<span>20</span></p>
    </td>
    <td class="cart_quantity">
        <div class="cart_quantity_button">
            <a class="cart_quantity_up" href="#"> + </a>
            <input class="cart_quantity_input" type="text" name="quantity" value="8" autocomplete="off" size="2">
            <a class="cart_quantity_down" href="#"> - </a>
        </div>
    </td>
    <td class="cart_total">
        <p class="cart_total_price"> ₹<span>160</span></p>
    </td>
    <td class="cart_delete">
        ...
    </td>
</tr>

I just need the sum of each (cart_price>span>p)*(.cart_quantity_input.val()).

PS:alert($d.length); is just to check no of tr's while testing but I need sum not count

3
  • first of all what element $table is in this code.? Commented Aug 24, 2015 at 5:40
  • $table is the <table> Commented Aug 24, 2015 at 5:42
  • then your syntax says that you are trying to get the value of each table 's value as DGS said you should try his solution Commented Aug 24, 2015 at 5:43

4 Answers 4

2

You need to first iterate over each tr then find the values .

$array=$table.find('tr').each(function(){
    var price = Number($(this).find('.cart_price>p>span').html());
    var qty = Number($(this).find('.cart_quantity_input').val());
    return price*qty;
}).toArray();
alert($array.length);

Or to calculate Sum:

total=0;
$table.find('tr').each(function(){
    var price = Number($(this).find('.cart_price>p>span').html());
    var qty = Number($(this).find('.cart_quantity_input').val());
    total+= price*qty;
});
alert(total);
Sign up to request clarification or add additional context in comments.

3 Comments

Could you format the code so its easier to read and understand?
Initially I thought it will be better to use the formatting used by the asker but yes it would be nice if any one can easily read it,thanks for suggestion
Nice, looks a lot better now. Would upvote again if i could :P
0

Assuming $table is a your table $table.each() will only run once on the table.

To have your function execute for each table row use

$table.find('tr').each()

Comments

0

You can iterate over each tr in the table and sum op the values

var sum = 0;
$table.find('tr').each(function () {
    sum += ($(this).find('.cart_price > p > span').text() * $(this).find('.cart_quantity_input').val()) || 0;
});
alert(sum)

Comments

-1
function getTotalPrice(){
var quantity =$('#cart_quantity_input').val();
var price= $('#cart_price').text();

var total = quantity * price;
alert(total);
}

Just try using id's this worked for me.

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.