Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize two text box form values?

Tags:

jquery

Hi all i am new to jQuery. Suppose I have two HTML text boxes. How can I make it happen that if I write in text box A then same value goes to textbox B and if I write in B then it goes to A and same as delete the text. How can this be done in jQuery?

like image 312
ali nouman Avatar asked Sep 22 '11 14:09

ali nouman


3 Answers

This is a more dynamic way using jQuery:

$(document).ready(function() {
  $(".copyMe").keyup(function() {
    $(".copyMe").val($(this).val());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="copyMe" type="text" placeholder="Enter a text"/>
<input class="copyMe" type="text" />
<input class="copyMe" type="text" />
<input class="copyMe" type="text" />
like image 151
JNDPNT Avatar answered Oct 26 '22 04:10

JNDPNT


This is quite easy:

$("#textBoxA").keyup(function(){
   $("#textBoxB").val($("#textBoxA").val());
});

$("#textBoxB").keyup(function(){
   $("#textBoxA").val($("#textBoxB").val());
});
like image 43
Snicksie Avatar answered Oct 26 '22 04:10

Snicksie


A slightly more efficient way than the most upvoted answer is:

var $inputs = $(".copyMe");  
$inputs.keyup(function () {
     $inputs.val($(this).val());
});

http://css-tricks.com/snippets/jquery/keep-text-inputs-in-sync/

like image 44
Barka Avatar answered Oct 26 '22 04:10

Barka