I currently have a simple numpad that's setup to insert a dollar amount into the span tag .bill-amount__integer
. The default text within this span reads as 0.00
.
<div class="bill-amount__value">
<span>$</span><span class="bill-amount__integer">0.00</span>
</div>
Right now the numeric digits of each button is appended to the span in a stack-like fashion LIFO which is seen in the first and last blocks of the conditional below.
$(".numpad__button").on("click", function() {
var that = this,
$that = $(that);
if ($that.hasClass("numpad__button--backspace")) {
$(".bill-amount__integer").text(function(index, value) {
return value.slice(0, -1);
});
} else if ($that.hasClass("numpad__button--return")) {
if ($(".bill-amount__integer").text().length === 0) {
$(".bill-amount__value").hide(0, function() {
$(this).prev().show()
});
}
$(".numpad").removeClass("numpad--raised");
} else {
$(".bill-amount__integer").text(function(index, value) {
return value += that.innerText;
});
}
});
What I'd like to do is replace the 0
and insert the selected integer starting from the right and working its way to the left. So for instance:
0.05 // If I insert the number 5
0.50 // If I insert numbers 5 and 0
5.50 // If I insert numbers 5,0,0
50.50 // If I insert numbers 5,0,0,0
The same methodology would work for removing numbers but re-inserting 0
in its place (reversed).
50.50 // Starts out at 50.50 and is reduced in the sequence below
5.05
0.50
0.05
I addition to inserting/removing integers I also wanted to add a comma for every three digits. I managed to do this using the regex below but didn't exactly how to integrate it with what I'm looking to achieve:
$(".numpad__button").on("click", function() {
...
$(".bill-amount__integer").text(function(i, v) {
return v.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
});
replace
method inside thetext
method to remove the.
and stop at 4 digits? To see what I'm talking about uncomment the code in the click event. – Carl Edwards May 18 '15 at 14:36