Add numbers within input in sequence when clicking

0

I have this input

<input type="text" readonly="readonly" class="form-control form-sm input_digitacao">

And I have this script

$(".numeros_ligacao li a").click(function(){
    digito = $(this).html();
    $(".input_digitacao").val(digito);
})

The variable digit returns me a single number, when clicking, inside that input the corresponding number appears, but I want it to add one after the other, not that it replaces as it is doing. Do you have any way?

    
asked by anonymous 26.03.2018 / 22:31

2 answers

2

It's simple, just do it this way:

$(".numeros_ligacao li a").click(function(){
    digito = $(this).html();
    var valor_anterior = $(".input_digitacao").val();
    $(".input_digitacao").val(valor_anterior + "" + digito);
});
    
26.03.2018 / 22:35
1

You can do it this way too, by taking the existing value in input :

$(".numeros_ligacao li a").click(function(){
    var digito = $(this).html();
    $(".input_digitacao").val(function(){ return $(this).val()+digito; });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ulclass="numeros_ligacao">
   <li>
      <a href="javascript:void(0)">1</a>
   </li>
   <li>
      <a href="javascript:void(0)">2</a>
   </li>
</ul>

<input type="text" readonly class="form-control form-sm input_digitacao">
    
27.03.2018 / 00:10