1

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="">
4
  • What language is this? Mar 1, 2015 at 20:48
  • javascript, but if there's another way, I would greatly appreciate it. Mar 1, 2015 at 20:54
  • So you want one input to be filled with two inputs ? I don't get what you are expecting here.
    – Telokis
    Mar 1, 2015 at 21:01
  • Yes! I need one input to be filled with two input values. Mar 1, 2015 at 21:10

2 Answers 2

9

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="">

2
  • Thank you so much, this worked! You help making this site awesome! Mar 1, 2015 at 22:20
  • Thank Ninetainedo. name mistake (name="result"). Aug 23, 2016 at 4:49
0

$("#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="">

$("#field1, #field2").keyup(function(){
    update();
});

function update() {
  var a = $('#field1').val();
  var b = $('#field2').val();

  $("#result").val('x');
  
}
<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="">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.