I want create a property inside my object with all jQuery objects which it will use. I am a novice at JavaScript and that’s why I want an advice. Can I do this in such a manner?
var myVar = {
init: function() {
//...
this.obj = {
id1: $('#id1'),
id2: $('#id2'),
id3: $('#id3')
}
this.method2();
},
method1: function() {
alert(this.obj.id1.html()) // some work with id1
},
method2: function() {
alert(this.obj.id1.html()) // some work with id1
}
};
$(function() {
myVar.init();
myVar.method1();
});
Your code seems fine. Only problem I could foresee with your code is that perhaps the DOM isn’t ready when it is running:
Example: http://jsfiddle.net/2HppG/1/
(In the example, I removed the standard DOM Ready wrapping that jsFiddle provides by default.)