Home > other >  How to add a value number when press the button?
How to add a value number when press the button?

Time:12-22

How to add number value when i click button? For example If i click 100,000 twice, 200,000 will be entered in the input.

$(document).ready(function() {
  $('#myform button').on('click', function() {
    $('#cvalue').val($(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<div >
  <form id="myform">
    <div >
      <input type="text" id="cvalue" >
    </div>
    <button  type="button" value="100,000">100,000</button>
    <button  type="button" value="200,000">200,000</button>
    <button  type="button" value="300,000">300,000</button>
    <button  type="button" value="500,000">500,000</button>
    <button  type="button" value="1,000,000">1,000,000</button>
  </form>

CodePudding user response:

Not trivial

const fmt = new Intl.NumberFormat('en-US', { minimumFractionDigits: 0, maximumFractionDigits: 0 });

$(document).ready(function() {
  const $cvalue = $('#cvalue');
  $('#myform .button1').on('click', function() {
    const val =  $(this).val().replace(/\D/g, "")    $cvalue.val().replace(/\D/g, "")
    $cvalue.val(fmt.format(val));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<div >
  <form id="myform">
    <div >
      <input type="text" id="cvalue"  value="0">
    </div>
    <button  type="button" value="100,000">100,000</button>
    <button  type="button" value="200,000">200,000</button>
    <button  type="button" value="300,000">300,000</button>
    <button  type="button" value="500,000">500,000</button>
    <button  type="button" value="1,000,000">1,000,000</button>
  </form>

  • Related