var a; var b; function update(input) { a = $(input); b = a.val().length; // далее код с переменные работает } 

here is the function call

  $("#jack").click(function () { update(this); }); 

But immediately upon exiting the function, these variables are equal to undefined. Why are they reset?

  • And where is the call to the function that will change them? By the way, global variables are evil. - user207618
  • Well, show me where you check these variables. Inside the click handler? - vp_arth
  • @vp_arth - One hundred against one, that variables are checked / displayed immediately after the function definition is defined function update(input) { ... } . - Igor

1 answer 1

Not equal to undefined . It's ok.

Check where you call the function and get the variables. If you call later than you receive, it will be undefined

Call before receiving:

 var a; var b; function update(input) { a = $(input); b = a.val().length; // далее код с переменные работает } update('.input'); console.log('a: '+a); console.log('b: '+b); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="input"> 

Call after receiving:

 var a; var b; function update(input) { a = $(input); b = a.val().length; // далее код с переменные работает } console.log('a: '+a); console.log('b: '+b); update('.input'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="input">