How to take the value of one input and assign it to another?

Asked

Viewed 22,610 times

0

Galera I have a form with several input that follow a sequence of pair, ie: value1 for cost1, value2 for cost2 and etc...

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

But I have several inputs and think about creating a script where I will do the attribution, 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 is that I don’t 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'>

2 answers

3


You can update the relative cost while typing, for example:

HTML (create a "value" class and a "cost" attribute to make it a little 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' >

  • thank you very much for the help, it worked perfectly

  • well I have one more question, without wanting to abuse more you can help me? I created an input called cost, where I will inform a value, then I have the inputs called value and margin, where I will enter a value in the margin in % and it will fill the value based on cost. Could you help me? Follow the code: https://jsfiddle.net/7aq46h0x/2/

  • What would be the calculation?

  • ok, it would be like this: I would inform the cost, right after I would inform a % in the margin field, the java has to take the cost see what the updated value based on the profit margin and put in the corresponding value field. Example of the java calculation: {var valor_magem = Math.round(cost * margin) / 100; var new_value = cost + value_act;}

  • See if that’s it: https://jsfiddle.net/7aq46h0x/5/

  • that’s right :) friend got very good. Just one more thing, is it possible to do the reverse tbm? type if I inform a value it tells me what profit margin obtained. has as?

  • You do. https://jsfiddle.net/7aq46h0x/6/

  • cool, but what I wanted to know is if there’s any way to keep the two forms together, that is at the same time. if I inform the margin it generates the value, if I inform the value it generates the margin. This is possible?

  • Yes, just merge the last two fiddles. https://jsfiddle.net/7aq46h0x/7/

  • friend, got very good. code got very simple. Thanks and congratulations.

  • just one more hahaha doubt. I’m trying to make him write the value in the en standard, ie: 400.33. I tried the following: {inputValor = inputValor.toString(). replace('.', ',');} but it didn’t work out, have any idea how to do it?

  • https://jsfiddle.net/7aq46h0x/8/

  • perfect, worked 100%

  • Could you give me a hand with this problem here? http://answall.com/questions/115916/problema-ao-formatter-numbers-com-javascript

  • hello without wanting to abuse, I could use a hand with this question here: http://answall.com/questions/116529/como-resolve-problema-de-calciulo-de-margem-javascript

Show 11 more comments

0

Just set the property value 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'/>

  • thanks for the help :)

Browser other questions tagged

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