skip to Main Content

I want to increase the value entered in the amount input by 10% in real time and show it in the amount + commission input, how can I do it? I will be glad if you help me thank you

inputs

2

Answers


  1. You need to provide the code you have so we can help you.

    However, you can make this with Jquery using .change() method

    let amount = 0;
    let amountPlusCommission = 0;
    
    const commission = 0.1;
    
    $("#yourinputID").change(function () {
        const value = $(this).val();
        amount = value;
        amountPlusCommission = value + (value * commission);
        $("#yourSecondinputID").val(amountPlusCommission);
    }); 
    

    Change the yourinputID with the first input ID and yourSecondinputID with the result input ID.

    EXTRA: .keyup() function is much better for this

    $("#yourinputID").keyup(function () {...})
    

    .change() detects when the input changes, but .keyup() detects when the user releases a key on the input and is much more efficient.

    Login or Signup to reply.
  2. Using jQuery, read the input value on change and then calculate the final amount after commission and display it in the second input field which will be disabled.

    $('input').on( "input", function() {
      console.log( $( this ).val() );
      let val = parseInt($(this).val());
      let comm = 10;
      let total = val + val*comm/100;
      $('input[name="total"]').val(total);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="number" name="amount"/>
    <input type="text" name="total" disabled />
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search