Hi Team
I have a logic to remove the item from the list, whilestill if a user add and decrease it does that part fine. The issue is when remove its not clickable, there are no bugs no error when inspecting. Yet another is its the updating of cart is not sequencial it does not start from 0 upwards it keeps changing the value and quantity does that as well.
// js code
JavaScript
*/
$(document).ready(function() {
// Add to cart button click event
$('.btn-plus').on('click', function(e) {
e.preventDefault();
var quantityInput = $(this).closest('.quantity').find('input');
var quantityValue = parseInt(quantityInput.val());
// Increase quantity value and update input
quantityInput.val(quantityValue + 1);
// Update cart value and total
updateCart();
});
// Remove from cart button click event
$('.btn-minus').on('click', function(e) {
e.preventDefault();
var quantityInput = $(this).closest('.quantity').find('input');
var quantityValue = parseInt(quantityInput.val());
// Decrease quantity value and update input
if (quantityValue > 0) {
quantityInput.val(quantityValue - 1);
// Update cart value and total
updateCart();
}
});
// Remove button click event
$('.btn-remove').on('click', function(e) {
e.preventDefault();
// Remove the corresponding row from the table
$(this).closest('tr').remove();
// Update cart value and total
updateCart();
});
// Function to update cart value and total
function updateCart() {
var cartValue = 0;
var cartTotal = 0;
// Loop through each product row
$('tbody tr').each(function() {
var quantityInput = $(this).find('input');
var quantity = parseInt(quantityInput.val());
var price = parseFloat($(this).find('.align-middle:nth-child(2)').text().replace('R', ''));
// Set cart value to the sum of all quantity values
cartValue += quantity;
// Calculate the total price for this row and add it to the cart total
var rowTotal = quantity * price;
cartTotal += rowTotal;
// Update the total for this row
$(this).find('.align-middle:nth-child(4)').text('R' + rowTotal.toFixed(2));
});
// Update cart value and total in the HTML
$('.badge').text(cartValue);
$('.cart-total').text('R' + cartTotal.toFixed(2));
}
// Initialize the cart value and total on page load
updateCart();
});// html code
Code
Colorful Stylish Shirt
R150
R150
Naimish MakwanaRank #324 · 7.7K reputationPosted May 11, 2023, 7:42 AM EDT
The problem with your code is that you are trying to remove the element from the table using the element.
.remove()method, but this method only works on elements that are descendants of the current element. In your case, the.remove()method is being called on the.btn-removebutton, which is not a descendant of theTo fix this, you can use the element to the JavaScript
GUJavaScript Know the answer? Forums 01
Arun PottiAnswers in the forums20answers02
Tahir AnsariAnswers in the forums5answers03
Kamlesh KumarAnswers in the forums5answers04
Sam HobbsAnswers in the forums4answers05
Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.Join Full leaderboard → 2026
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
.closest()method to get the closest.btn-removebutton, and then use the.remove()method on that element.Here is the updated code:
$(document).ready(function() { // Add to cart button click event $('.btn-plus').on('click', function(e) { e.preventDefault(); var quantityInput = $(this).closest('.quantity').find('input'); var quantityValue = parseInt(quantityInput.val()); // Increase quantity value and update input quantityInput.val(quantityValue + 1); // Update cart value and total updateCart(); }); // Remove from cart button click event $('.btn-minus').on('click', function(e) { e.preventDefault(); var quantityInput = $(this).closest('.quantity').find('input'); var quantityValue = parseInt(quantityInput.val()); // Decrease quantity value and update input if (quantityValue > 0) { quantityInput.val(quantityValue - 1); // Update cart value and total updateCart(); } }); // Remove button click event $('.btn-remove').on('click', function(e) { e.preventDefault(); // Get the closest tr element to the btn-remove button var tr = $(this).closest('tr'); // Remove the tr element from the table tr.remove(); // Update cart value and total updateCart(); }); // Function to update cart value and total function updateCart() { var cartValue = 0; var cartTotal = 0; // Loop through each product row $('tbody tr').each(function() { var quantityInput = $(this).find('input'); var quantity = parseInt(quantityInput.val()); var price = parseFloat($(this).find('.align-middle:nth-child(2)').text().replace('R', '')); // Set cart value to the sum of all quantity values cartValue += quantity; // Calculate the total price for this row and add it to the cart total var rowTotal = quantity * price; cartTotal += rowTotal; // Update the total for this row $(this).find('.align-middle:nth-child(4)').text('R' + rowTotal.toFixed(2)); }); // Update cart value and total in the HTML $('.badge').text(cartValue); $('.cart-total').text('R' + cartTotal.toFixed(2)); } // Initialize the cart value and total on page load updateCart(); });Thanks
Guest UserRank #877 · 2.5K reputationPosted May 11, 2023, 7:51 AM EDT
Naimish
the current code is not yet working, my apology on my side for oversight sending incomplete table. here is the corrected table uses remove *.
Products Price Quantity Total Remove Colorful Stylish Shirt R150 R150 Colorful Stylish Shirt R150 R150 Colorful Stylish Shirt R150 R150 Colorful Stylish Shirt R150 R150 Colorful Stylish Shirt R150 R150Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts