Advertisement
sourav8256

Untitled

Jul 30th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>My Ecommerce App - Cart</title>
  5. <!-- Add the Bootstrap CSS link -->
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <header>
  10. <nav class="navbar navbar-expand-lg navbar-light bg-light">
  11. <a class="navbar-brand" href="#">My Ecommerce App</a>
  12. <!-- Add your navigation links here, if needed -->
  13. </nav>
  14. </header>
  15.  
  16. <div class="container mt-4">
  17. <h2>Shopping Cart</h2>
  18. <table class="table">
  19. <thead>
  20. <tr>
  21. <th scope="col">Product</th>
  22. <th scope="col">Price</th>
  23. <th scope="col">Quantity</th>
  24. <th scope="col">Subtotal</th>
  25. <th scope="col"></th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <tr>
  30. <td>Product 1</td>
  31. <td>$100</td>
  32. <td>
  33. <input type="number" class="form-control quantity-input" value="1" min="1">
  34. </td>
  35. <td>$100</td>
  36. <td>
  37. <button type="button" class="btn btn-danger btn-sm delete-product">Remove</button>
  38. </td>
  39. </tr>
  40. <tr>
  41. <td>Product 2</td>
  42. <td>$50</td>
  43. <td>
  44. <input type="number" class="form-control quantity-input" value="1" min="1">
  45. </td>
  46. <td>$50</td>
  47. <td>
  48. <button type="button" class="btn btn-danger btn-sm delete-product">Remove</button>
  49. </td>
  50. </tr>
  51. <!-- Add more products here as needed -->
  52. </tbody>
  53. <tfoot>
  54. <tr>
  55. <td colspan="3"></td>
  56. <th>Total:</th>
  57. <td id="totalPrice">$150</td>
  58. </tr>
  59. </tfoot>
  60. </table>
  61. </div>
  62.  
  63. <!-- Add the Bootstrap JS and jQuery links -->
  64. <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
  65. <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.6.0/dist/umd/popper.min.js"></script>
  66. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  67.  
  68. <script>
  69. $(document).ready(function() {
  70. // Update the subtotal and total price when the quantity changes
  71. $(".quantity-input").on("change", function() {
  72. const quantity = parseInt($(this).val());
  73. const price = parseFloat($(this).closest("tr").find("td:nth-child(2)").text().replace("$", ""));
  74. const subtotal = quantity * price;
  75. $(this).closest("tr").find("td:nth-child(4)").text("$" + subtotal.toFixed(2));
  76. updateTotalPrice();
  77. });
  78.  
  79. // Remove the product from the cart
  80. $(".delete-product").on("click", function() {
  81. $(this).closest("tr").remove();
  82. updateTotalPrice();
  83. });
  84.  
  85. // Function to update the total price
  86. function updateTotalPrice() {
  87. let totalPrice = 0;
  88. $(".quantity-input").each(function() {
  89. const quantity = parseInt($(this).val());
  90. const price = parseFloat($(this).closest("tr").find("td:nth-child(2)").text().replace("$", ""));
  91. const subtotal = quantity * price;
  92. totalPrice += subtotal;
  93. });
  94. $("#totalPrice").text("$" + totalPrice.toFixed(2));
  95. }
  96. });
  97. </script>
  98. </body>
  99. </html>
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement