I'll admit that I don't really know what I'm doing. But I'm trying to work with JS in an OO way.
I have a form field where I input data. On the right side I have a "preview" of how the special offer will look like.
As I add text in the input fields, I want it to show in the preview.
I've tried using the following code, but I'm not being very successful. I think there are some basic knowledge / understanding I'm missing here.
mirrorText: function(){
var self = this;
$('#title').keyup(function(){
self.previewObject.title.text($(this).val());
});
},
previewObject: {
img: null,
title: null,
dateRange: null,
address: null,
init: function(){
self.img = $('.offer-container .promo-img');
self.title = $('.offer-container h3');
self.dateRange = $('.offer-container .valid-through');
}
}
See the full code here (jsfiddle).
selfreferences inpreviewObject.init(). There is noself. There is onlythis. :)selfbecause others have adviced it: stackoverflow.com/questions/337878/var-self-thisselfis used when you need to retain the outer scope in the inner scope. For example, the ideal way to useselfis what you have done inmirrorText(). Also, in your case, self was undeclared in the scope ofpreviewObjectas well aspreviewObject.init(), which is where you would getReferenceErrors.