javascript - JQuery adding object function using .on() -
i'm trying add object's function button created when conditions met. matter if have var i'm trying change outside document.ready?
var myvar = 1; $(document).ready(function(){ var myobj = { changemyvar: function(){ myvar++;} } ... /* button id='my-button' created in div id='mydiv' */ ... $('#mydiv').on('click', '#my-button', myobj.changemyvar); });
no function attached button , no error thrown. in fact, console.log(myobj.changemyvar) returns undefined. missing here?
edit: lied no errors, 'undefined not function' on .on() line.
thanks!
edit2: after further analysis, discovered silly type-o in original code. answers!
i tried replicating issue in fiddle, worked.
try out http://jsfiddle.net/7fjk3wxs/1/
this used:
var myvar = 1; $(document).ready(function(){ var myobj = { changemyvar: function(){ myvar++; alert(myvar); } }; $('#mydiv').on('click', '#my-button', myobj.changemyvar); });
Comments
Post a Comment