i have the below code:
var LabourItems = {
rate: null,
hours: null,
total: null,
init: function(object) {
var rate = $(object).children('.rate').first();
var hours =$(object).children('.hours').first();
total = rate * hours;
updateTotal(object,total);
},
updateTotal: function(object, total) {
$(object).children('.total').first().attr('value', total)
}
}
//reactTochange for those inputs that you want to observe
$('.hours').live("click", function() {
jQuery.each($('.labouritems'), function(key,value){
LabourItems.init(value);
});
});
and geting an undefined function error:
updateTotal(object, total) is undefined
Could you please let me know what i'm doing wrong.
1 Answer 1
updateTotal is a function in LabourItems so call
LabourItems.updateTotal(object,total);
or ( only when invoked from any other function of LabourItems or instances of it)
this.updateTotal(object,total);
UPDATE :
var LabourItems = {
rate: null,
hours: null,
total: null,
init: function(object) {
var rate = $(object).children('.rate').first();
var hours =$(object).children('.hours').first();
this.total = rate * hours;
this.updateTotal(object);
},
updateTotal: function(object) {
$(object).children('.total').first().attr('value', this.total)
}
}
//reactTochange for those inputs that you want to observe
$('.hours').live("click", function() {
jQuery.each($('.labouritems'), function(key,value){
LabourItems.init(value);
});
});
answered Oct 18, 2012 at 7:54
Diode
25.2k8 gold badges43 silver badges52 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Barrie Reader
Not only this, but I'm pretty sure that
object is a reserved word...?user979587
this worked but when i create new Labouritems rows they do not pick up the correct total field, any tips?
Diode
Ok. So you are making instances. Then you have to access methods and properties using
this. try this.total .Diode
currently in your code
total is global, so all instances update the same totaluser979587
i updated them all the totals to 'this.total', not it updates every total on the page.
lang-js