I'm trying to add an object's function to a button that is created when some conditions are met. Does it matter if I have the var I'm trying to change outside document.ready?
var myvar = 1;
$(document).ready(function(){
var myobj = {
changeMyVar: function(){ myvar++;}
}
...
/* button id='my-button' is created in a div id='mydiv' */
...
$('#mydiv').on('click', '#my-button', myobj.changeMyVar);
});
No function is attached to the button and no error is thrown. In fact, console.log(myobj.changeMyVar) returns undefined. What am I missing here?
Edit: I lied about no errors, I get a 'undefined is not a function' on the .on() line.
Thanks!
Edit2: After further analysis, I discovered a silly type-o in my original code. Thanks for the answers!