-1

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

8

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);
   });
});
Sign up to request clarification or add additional context in comments.

5 Comments

Not only this, but I'm pretty sure that object is a reserved word...?
this worked but when i create new Labouritems rows they do not pick up the correct total field, any tips?
Ok. So you are making instances. Then you have to access methods and properties using this. try this.total .
currently in your code total is global, so all instances update the same total
i updated them all the totals to 'this.total', not it updates every total on the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.