javascript - Function parameter not getting updated inside click event -
q: function parameter not getting updated within click event
**event.js** // main click event phone call $(document).on('click', '.start', function(){ root.ajax({ url: 'location' }, function( response ){ root.update( response ); }) }); **content.js** var flag = false; root.update = function( response ){ if(!flag){ // event assignment new created button $(document).on('click', '.innerstart', function(){ // first time prints okay after printing old value // response not getting updated console.log( response ); }); flag = true; } }
basically, response
variable passed in first time. set click event handler, logs response, , never set click handler again.
that response variable never changed - 1 set in original click handler used, because that's value passed in. instead, seek set variable, like:
**event.js** var response; // main click event phone call $(document).on('click', '.start', function(){ root.ajax({ url: 'location' }, function( responsevalue ){ root.update( responsevalue ); }) }); **content.js** var flag = false; root.update = function( responsevalue ){ response = responsevalue; if(!flag){ // event assignment new created button $(document).on('click', '.innerstart', function(){ // first time prints okay after printing old value // response not getting updated console.log( response ); }); flag = true; } }
javascript jquery javascript-events
No comments:
Post a Comment