R.D.
R.D.

Reputation: 7403

copy text box content to another textbox while typing

I writing j query which copy content from one text box into another. am not an expert in j query following in mine code

    $(function() {
    $('input[id$=tb1]').keyup(function() {
        var txtClone = $(this).val();
        $('input[id$=txtCustName]').val(txtClone);
    });
});

Upvotes: 3

Views: 12082

Answers (3)

Prasant Kumar
Prasant Kumar

Reputation: 1058

It's Simple. Just write these codes.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <input type="text" name="text1" id="text1" value="" /> <br >
    <input type="text" name="text2" id="text2" value="" />
<script>
    $(document).ready(function(){
        $('#text1').keyup(function(){
        $('#text2').val($(this).val());
      });
    });
</script>

You can see it here https://jsfiddle.net/prasant200582/x7jzk0pg/5/

Upvotes: 0

JKirchartz
JKirchartz

Reputation: 18022

try this js:

$('input[id$=tb1]').on('keyup',function() {
    $('input[id$=txtCustName]').val($(this).val());
});

use jQuery's on() to bind to the event is much better, and you don't have to set the val to a variable first...

EDIT

the above code will clone the content into any field ending with txtCustName if you have html like:

<input id="random_tb1"/>
<input id="text_txtCustName"/>
<input id="other_tb1"/>
<input id="stuff_txtCustName"/>

it has no idea which one you want, so if you make your html something like this:

<div>
    <input id="random_tb1"/>
    <input id="text_txtCustName"/>
</div>
<div>
    <input id="other_tb1"/>
    <input id="stuff_txtCustName"/>
</div>

you can keep them separated in html, and only update the related field with this JS:

$(function() {

    $('input[id$=tb1]').on('keyup',function() {
        $('input[id$=txtCustName]',$(this).parent()).val($(this).val());
    });

});​

here's a demo: http://jsfiddle.net/JKirchartz/XN2qD/

Upvotes: 9

Greg
Greg

Reputation: 31378

There doesn't seem anything wrong with the code you have supplied.

Ive recreated it using JSFiddle

http://jsfiddle.net/e9KFT/

Could we have more info? A snippet of your HTML markup would be a good start!

Upvotes: 0

Related Questions