How do I get the value of one input and assign it to another?

0

I have a form with multiple input that follows a pair sequence, ie: value1 for cost1, value2 for cost2 and etc ...

I need to get the input value value1 and assign the same value to cost1.

But I have several inputs and think of creating a script where I will do the assignment, and in each input I call the event and inform the name of the input, the script goes and assigns the value in the desired input.

The problem and I do not know how to do this, can anyone help me?

<input type='text' id='valor1' name='valor1'>
<input type='text' id='custo1' name='custo1'>

<br>
<br>

<input type='text' id='valor2' name='valor2'>
<input type='text' id='custo2' name='custo2'>
    
asked by anonymous 18.02.2016 / 18:17

2 answers

3

You can update the relevant cost as you type, for example:

HTML (create a "value" class and a "cost" attribute to make it a bit more dynamic):

JAVASCRIPT:

    $(document).ready(function(){
        $(".valor").on("input", function(){
            var textoDigitado = $(this).val();
            var inputCusto = $(this).attr("custo");
            $("#"+ inputCusto).val(textoDigitado);
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type='text' id='valor1' class='valor' name='valor1' custo='custo1'>
<input type='text' id='custo1' name='custo1' >

<input type='text' id='valor2' class='valor' name='valor2' custo='custo2'><br>
<input type='text' id='custo2' name='custo2' >
    
18.02.2016 / 18:25
0

Just set the value property of the element:

var a = document.getElementById('a');
var b = document.getElementById('b');

b.value = a.value;
<input id='a' type='text' value='StackOverflow'/>
<input id='b' type='text'/>
    
18.02.2016 / 18:25