Henrik Baecklund Henrik Baecklund - 10 months ago 81
Javascript Question

Subtract number in span

I would like

5 left
e.g. become
4 left


<span class="small">5 left</span>


// Store the 5 left in "numberStock" variable
var numberStock = parseInt($(".small:first").text());

// Subtract with "numberStock -1"
var minusStock = numberStock - 1;

// Here comes the issue, how do I add the new content?
$(".small:first").attr(minusStock "left");


How do I add the new numberStock number
and text

Answer Source

i am not sure if i understood your question, but if i did here is a way to do it , which is pretty close to what you were trying to achieve

var numberStock = parseInt($('.small').text())
var minusStock = numberStock - 1;
$('.small').text(minusStock + ' left');

here is a fiddle in case you want to test around with it https://jsfiddle.net/09wcjp7b/