javascript - Changing global variable value inside function -


here simple scenario. have variable defined inside ready() function assigns first value.

i have function outside ready(). want use changed variable inside new function.

here javascript code:

var myfunction = function() {     // wanna change vp value here , wanna      // use function new value             vp = "new value";      myfunction2 ();  };  $(document).ready(function () {      var vp = "first value asign";     $('#btnaddcustomer').click(myfunction);      var myfunction2 = function() {         // use vp variable here new value     }; }); 

discard var statement. assigned value new local variable, instead of global variable.

here:

var vp = "first assigned value" var myfunction2;  var myfunction = function() {     // wanna change vp value here , wanna use function new value              vp =  "new value"     myfunction2();       };  $(document).ready(function () {      vp = "first value asign";      $('#btnaddcustomer').click(myfunction);      myfunction2 = function() {      alert(vp)      };  }); 

fiddle: http://jsfiddle.net/xggvv/10/

now works.

a nice reading on js variable scopes: http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -