javascript - Building rudimentary data binding with jQuery but variables are not scoping properly -


i attempting replicate basic data binding view template, controller. building using straight js , jquery. there variable in controller function want change when user inputs field. events firing variable remains unchanged. whats best way scope variables work:

function mycontroller() {     var password = '1234';      //handle data binding  bindto = function(elem, prop) {      console.log('original stored prop = ' + prop); //this logs out 1234              //but first time             //on every subsequent keypress logs out value user types             //which has no effect on var password in controller                      var elem = $(elem);     var value = elem.val();       // save current value of element     elem.data('oldval', value);      // changes in value     elem.bind("propertychange keyup input paste", function(event){         console.log('inside bind fn prop = ' + prop)     var val = elem.val();     // if value has changed...     if (elem.data('oldval') != val) {         // updated stored value         elem.data('oldval', val);         prop = val; //where prop being set?         console.log(prop); //this logs value      }    });  };  //set bindings bindto($('input.loginpassword')[0], password); } 

try

function mycontroller(elem, prop) { var password = '1234';   bindto = function(elem, prop) {  console.log('original stored prop = ' + prop); //this logs out 1234          //but first time         //on every subsequent keypress logs out value user types         //which has no effect on var password in controller                  var elem = $(elem); var value = elem.val();   // save current value of element elem.data('oldval', value);  // changes in value elem.bind("propertychange keyup input paste", function(event){     console.log('inside bind fn prop = ' + prop) var val = elem.val(); // if value has changed... if (elem.data('oldval') != val) {     // updated stored value     elem.data('oldval', val);     prop = val; //where prop being set?     console.log(prop); //this logs value  }  });       }(elem, prop); 

Comments