JSFiddle - React, Tailwind, and code Playground
HTML
<div class="goods">
<div class="name">Название товара</div>
<div class="goods-count">
<span class="amount">
<input type="button" class="down" value="-">
<input type="text" class="counter" value="1" size="4" data-price="5000" data-count="1"> шт
<input type="button" class="up" value="+">
</span>
</div>
<div class="goods-price"><span class="sum">5000 руб.</span></div>
</div>
JavaScript
$(document).ready(function() {
var counterSel = $('.counter');
var sumSel = $('.sum');
var unitPrice = counterSel.attr('data-price') / counterSel.attr('data-count');
var getCount = function() {
var result = parseInt(counterSel.val());
if (isNaN(result)) return 0;
if (result < 0) return 0;
return result;
}
var updatePrice = function() {
var totalPrice = getCount() * unitPrice;
sumSel.html(totalPrice + ' руб.');
};
var increment = function(count) {
var newCount = getCount() + count;
if (newCount < 0) newCount = 0;
counterSel.val(newCount);
updatePrice();
}
counterSel.on('change', updatePrice);
counterSel.on('keyup', updatePrice);
$('.down').on('click', function() {
increment(-1);
});
$('.up').on('click', function() {
increment(1);
});
});