Custom events with trigger()
Parts of the page talk through events, without knowing about each other.
jQuery · Custom events with trigger()Intermediate
$(document).on('cart:changed', function (e, count, total) {
$('#cart-count').text(count);
$('#cart-total').text(total.toFixed(2));
});
function addToCart(price) {
const count = Number($('#cart-count').text() || 0) + 1;
const total = Number($('#cart-total').text() || 0) + price;
$(document).trigger('cart:changed', [count, total]);
}
addToCart(9.5);
addToCart(5);triggercustom eventcommunication