the title gives error when use following:
var test = { elem : undefined, init : function() { console.log("test.init() fired"); elem = $("#id"); console.log("elem assigned jquery object"); elem.click(function() { console.log("elem clicked"); }); console.log("elem click handler created"); } }; test.init(); test.elem.click();
i don't understand. if run test.init()
assigns value test.elem
, why undefined when try access later?
you're creating new reference called 'elem' inside of init function rather referencing test.elem.
inside init need assign element 'this.elem', read:
init : function() { console.log("test.init() fired"); this.elem = $("#id"); console.log("elem assigned jquery object"); this.elem.click(function() { console.log("elem clicked"); }); console.log("elem click handler created"); }
Comments
Post a Comment