Search code examples
javascriptautofill

Autofill input with another input values


I need to autofill an input field with the values of another input fields. So far I have this:

$("#field1, #field2").keyup(function(){
    $("#result").val(this.value);
});

<input type="text" id="field1" name="field1" value="" >
<input type="text" id="field2" name="field2" value="" >

//* to be filled with the values of the inputs above *// 
<input type="text" id="result" name="resut" value="">

Solution

  • What about

    $("#field1, #field2").keyup(function(){
        update();
    });
    
    function update() {
      $("#result").val($('#field1').val() + " " + $('#field2').val());
    }
    

    $("#field1, #field2").keyup(function(){
        update();
    });
    
    function update() {
      $("#result").val($('#field1').val() + " " + $('#field2').val());
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" id="field1" name="field1" value="" >
    <input type="text" id="field2" name="field2" value="" >
    <br>
    <input type="text" id="result" name="resut" value="">