Setting a textbox text based on the click of another textbox using jquery

I have 64 textboxes named like this:

tbR1T1
tbR1T2
tbR1T3
tbR1T4
...
tbR1T64


Then I have a set of 32 more textboxes named like this
tbR2T1
tbR2T2
tbR2T3
tbR2T4
...
tbR2T32

I want to set up a jquery that when a user clicks on a textbox, a corresponding textbox is populated with the text from the texbox clicked.

so clicking tbR1T1 or tbR1T2 would populate tbR2T1
clicking tbR1T3 or tbR14 would populate tbR2T2
and so on

Is there a way to do this with one jquery and not one for each set of two textboxes? Or some similar way to handle this?
Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
here a live sample:
http://jsfiddle.net/EE_RainerJ/cfv6p8n6/

$(document).ready(function() {
    $("input[name^='tbR1T']").click(function() {
        SetTextboxValue($(this));
    });
});
    
function SetTextboxValue(originator) {
    var originatorName = $(originator).attr("name");
    var originatorNumber = parseInt(originatorName.replace("tbR1T",""),10);
    var targetFieldNumber = 0;
    if ((originatorNumber % 2) == 0)
    {
        targetFieldNumber = (originatorNumber/2);
    } else {
        targetFieldNumber = ((originatorNumber+1)/2);
    }
    $("input[name='tbR2T" + targetFieldNumber + "']").val($("input[name='tbR1T" + (targetFieldNumber*2-1)+ "']").val() + ' - ' + $("input[name='tbR1T" + (targetFieldNumber*2)+ "']").val());
}

Open in new window


HTH
Rainer
0
Mike MillerSoftware EngineerAuthor Commented:
Kinda close I think.
It shouldn't display both fields in the resulting textbox.

Clicking tbR1T1 or tbR1T2 would populate tbR2T1 would populate the text in the textbox clicked. Think of it this way...


tbR1T1
                  tbR2T1 (This would equal either tbR1T1 or tbR1T2, whichever is clicked)
tbR1T2



tbR1T3
                  tbR2T2 (This would equal either tbR1T3 or tbR1T4, whichever is clicked)
tbR1T4



tbR1T5
                  tbR2T3 (This would equal either tbR1T5 or tbR1T6, whichever is clicked)
tbR1T6


and so on...
0
Rainer JeschorCommented:
OK, sorry - I had a wrong understanding for that.
New fiddle:
http://jsfiddle.net/EE_RainerJ/bwgjz9ug/

Here the updated part of the script:
function SetTextboxValue(originator) {
    var originatorName = $(originator).attr("name");
    var originatorNumber = parseInt(originatorName.replace("tbR1T",""),10);
    var targetFieldNumber = 0;
    if ((originatorNumber % 2) == 0)
    {
        targetFieldNumber = (originatorNumber/2);
    } else {
        targetFieldNumber = ((originatorNumber+1)/2);
    }
    $("input[name='tbR2T" + targetFieldNumber + "']").val($(originator).val() );
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
Perfect!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.