How to program and calculate multiple subtotal and grandtotal using jquery?

Posted by Victor on Stack Overflow See other posts from Stack Overflow or by Victor
Published on 2010-05-08T21:46:28Z Indexed on 2010/05/08 22:08 UTC
Read the original article Hit count: 253

Filed under:

I'm stump figuring out how to do this in jquery, I need to do it without any plug-in. Imagine a shopping cart for books, each change of quantity (using select dropdown) will update the total price, grandtotal and then the hidden input value.

<table>
<tr> 
    <td class="qty"> 
        <select class="item-1">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        ...
        </select>
    </td> 
    <td class="product"> 
        Book 1 
    </td> 
    <td class="price-item-1"> 
        $20
    </td> 
    <td class="total-item-1"> 
        $20
    </td> 
</tr>
<tr> 
    <td class="qty"> 
        <select class="item-2">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        ...
        </select>
    </td> 
    <td class="product"> 
        Book 2 
    </td> 
    <td class="price-item-2"> 
        $10
    </td> 
    <td class="total-item-2"> 
        $10
    </td> 
</tr>
...
...
<tr> 
    <td colspan="3" align="right"> 
        <strong>Grand Total:</strong> 
    </td> 
    <td class="grandtotal">
    </td> 
</tr> 
</table>

<input type="hidden" id="qty-item-1" value="0"  />
<input type="hidden" id="total-item-1" value="0"  />

<input type="hidden" id="qty-item-2" value="0"  />
<input type="hidden" id="total-item-2" value="0"  />

© Stack Overflow or respective owner

Related posts about jQuery