Henrik Baecklund Henrik Baecklund - 4 days ago 6
Javascript Question

Subtract number in span

I would like

5 left
do
-1
e.g. become
4 left
.

HTML

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


jQuery

// 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");


Question

How do I add the new numberStock number
4
and text
left
?

Answer

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/

Comments