JQuery change values 2 input textbox
I want to create 2 text boxes that will change at the same time
<input type="text" class="text1">
<input type="text" class="text2">
They are created dynamically, so my code is:
$("body").on("keyup", function() {
$(".text2").val($('.text1').val());
});
It only works when changing text1. How can I write my script to change text2?
Thank!
+3
source to share
2 answers
You can do something like
var $ins = $('.text1, .text2').keyup(function() {
$ins.not(this).val(this.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="text1">
<input type="text" class="text2">
Here we are changing the value of an input field other than the one in which the keyup is performed for the updated value.
Using event delegation
$(document).on('keyup', '.text1, .text2', function() {
$('.text1, .text2').not(this).val(this.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="text1">
<input type="text" class="text2">
+5
source to share