Jquery – copy text box content to another textbox while typing

jquery

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);
    });
});

Best Solution

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/