I’m trying to add a thousands separator for the .htm(price * num)
result by adding .digits();
to the var sub
. How do I get the thousands seperator to work on the result of the var sub
? Since they are not val
do I need to convert the result into a number before adding the digits();
function?
$(document).ready(function() {
$.fn.digits = function() {
return this.each(function() {
$(this).val(
$(this)
.val()
.replace(/(d)(?=(ddd)+(?!d))/g, "$1,")
);
});
};
var total = $(".slider__total").html();
var sold = $(".sold").html();
var available = total - sold;
var price = $(".cost").html();
var num = $("#num").html();
$(".item__available").html(available);
var sub = $("#slider_subtotal")
.html(price * num)
.digits();
$(".qty").attr({
max: available
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
sold<span class="sold">3</span>total<span class="slider__total">10</span>available<span class="item__available"></span><input type="range" min="1" value="1" class="qty" name='quantity' oninput="num.value = this.value"><output id="num">0</output>unit:
$
<span class="cost">500</span>subtotal: $<span id="slider_subtotal">0</span>