Add numbers inside input in sequence by clicking

Asked

Viewed 178 times

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 this input appears the corresponding number, but I want you to add one after the other, not replace as you are doing. Is there any way?

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);
});

1

You can do it this way too, by taking the value already existing 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>
<ul class="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">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.