0

The customer may click a button with specified amount and the amount would display in input field. I'm trying to automatically add a comma on the displayed amount but it's only working if the amount is typed. What would be the easiest way to do it?

<input type="number" class="input-char-amo" id="d-total" step="10000" value="0" min='10000' max="5000000" / required>

https://codepen.io/Cilissaaa/pen/vYYGjYB

Cilissa
  • 15
  • 6

2 Answers2

1

You could use toLocaleString() method. It returns a string with a language-sensitive representation of a number.

let n = 1000000;
n.toLocaleString(); //"1,000,000"
rahool
  • 629
  • 4
  • 6
0

chek it once. Hope it helps.

I have found the solution on this link: Can jQuery add commas while user typing numbers?

$('input.number').keyup(function(event) {

  // skip for arrow keys
  if(event.which >= 37 && event.which <= 40) return;

  // format number
  $(this).val(function(index, value) {
    return value
    .replace(/\D/g, "")
    .replace(/\B(?=(\d{3})+(?!\d))/g, ",")
    ;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input class="number">
Kevin
  • 1,241
  • 7
  • 20
  • Thanks but I'm actually looking for an automatically add comma on the amount that was selected from a specified amount in a button. – Cilissa Oct 15 '19 at 07:21
  • Can you reproduce a fiddle .. it will help to understand better.. – Kevin Oct 15 '19 at 07:23