applying jquery variable to a label

I have a mvc 5 application.
I have a view called Home.cshtml

In my view I'm using jQuery

So in my jquery code on my view right now I have a text box and a label on my page.

Right now on the keyup event for my textbox i'm copying whatever value is typed into the textbox to a label like this:

This works fine.

            $("#TextBox1").on("keyup", function () {

                // copy the value from this text box to the label with id of Label1

In my jquery code i have also declared a variable like this which basically gets the value typed in another text box and sets the value to this variable.

var TestVariable1 = document.getElementById("Textbox2").value;

Anyone know how I can get the value  stored in that variable and apply that value to my label in the TextBox1 keyup event.?
So far I have this but I don't know the syntax. Anyone know?

            $("#TextBox1").on("keyup", function () {

                // on keyup of TextBox1 get the value of variable TestVariable1 and apply that variable value to the label called Label1
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.

>>In my jquery code i have also declared a variable like this...var TestVariable1 = document.getElementById("Textbox2").value;
Try declaring outside any function.  Then set the value as you have, but try getting rid of the "var" keyword.  If you have it declared within a function, then it is "private" to the function.
var TestVariable1; //declared outside any function

function someFunction(){

TestVariable1 = document.getElementById("Textbox2").value;

 $("#TextBox1").on("keyup", function () {
$("#Label1").text(TestVariable1);/* or you can also try $("#Label1").text( window.TestVariable1 ); */

Open in new window


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
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

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.