Transfer value from one input to another

Asked

Viewed 52 times

0

I have a field for general search and other segmented search fields (Pending, Current, Closed), I want everything typed in general to be automatically passed to the segmented ones.

I created this function that does this:

function myFunction() {
        $('#listaContratos').bootstrapTable('resetSearch', $('#myInput').val());
    }

Which is called when some value is typed in the general search:

<input class="form-control" id="myInput" onkeydown="myFunction()" type="text" placeholder="Pesquisar" />

However, the first time I call the function, there is still nothing in "myInput", so it always arrives in the targeted searches with a character less. Example: While overall I typed 2353, in segmented comes 235.

How do I, for when the function is called, also be passed the value I just typed?

  • Change the onkeydown event to onkeyup. When the onKeyDown event is called, the character has not yet been added to the input value.

  • @Danielzazula that’s right, thank you!

1 answer

2

Just copy the value in the On Key Up event from the source Input:

function transferirValor() {
    jQuery('#inputDestino').val(jQuery('#inputOrigem').val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Origem: <input type="text" name="inputOrigem" id="inputOrigem" onkeyup="transferirValor()" />
Destino: <input type="text" name="inputDestino" id="inputDestino" />

Browser other questions tagged

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