I'm trying to make a simple form that will allow a user to type into an HTML text box, which will then send that to another text box that they cannot modify. I have figured out how to do this; however, the code I'm using feels bloated and I'm sure there's a way to streamline it.
Here is my Javascript:
$(".name_input_1").on('keyup',function(){
$(".name_print_1").val($(this).val());
});
$(".name_input_2").on('keyup',function(){
$(".name_print_2").val($(this).val());
});
$(".name_input_3").on('keyup',function(){
$(".name_print_3").val($(this).val());
});
$(".name_input_4").on('keyup',function(){
$(".name_print_4").val($(this).val());
});
$(".name_input_5").on('keyup',function(){
$(".name_print_5").val($(this).val());
});
$(".name_input_6").on('keyup',function(){
$(".name_print_6").val($(this).val());
});
$(".name_input_7").on('keyup',function(){
$(".name_print_7").val($(this).val());
});
$(".name_input_8").on('keyup',function(){
$(".name_print_8").val($(this).val());
});
$(".name_input_9").on('keyup',function(){
$(".name_print_9").val($(this).val());
});
$(".name_input_10").on('keyup',function(){
$(".name_print_10").val($(this).val());
});
$(".name_input_11").on('keyup',function(){
$(".name_print_11").val($(this).val());
});
$(".name_input_12").on('keyup',function(){
$(".name_print_12").val($(this).val());
});
...and a little more...
function updateText(type) {
var id = type+'_print';
document.getElementById(id).value = document.getElementById(type).value;
}
Here is some HTML:
<div style="float:left;">
<input type="text" class="name_input_1">
<br>
<input type="text" class="name_input_2">
<br>
<input type="text" class="name_input_3">
<br>
<input type="text" class="name_input_4">
<br>
<input type="text" class="name_input_5">
<br>
<input type="text" class="name_input_6">
<br>
<input type="text" class="name_input_7">
<br>
<input type="text" class="name_input_8">
<br>
<input type="text" class="name_input_9">
<br>
<input type="text" class="name_input_10">
<br>
<input type="text" class="name_input_11">
<br>
<input type="text" class="name_input_12">
<br>
</div>
<div style="float:left;">
<input type="text" class="name_print_1" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_2" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_3" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_4" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_5" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_6" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_7" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_8" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_9" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_10" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_11" placeholder="Automatically Filled" disabled>
<br>
<input type="text" class="name_print_12" placeholder="Automatically Filled" disabled>
I have tried to make a Fiddle for it, but it doesn't work there. Not sure why. This does work just fine on a local machine. I'm just looking for a way to slim-down that code. Yes, I am a Javascript novice at best. Please go easy on me. :)
let names = ".name_input_1,.name_input_2,.name_input_3"and then something likenames.split(',').forEach(name => $(name).on('keyup',function(){ $(name).val($(this).val()); })). Still ugly, but less boilerplate.