Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:24:53+00:00 2026-06-13T08:24:53+00:00

i have the below code which i’m unable to populate the total field in

  • 0

i have the below code which i’m unable to populate the total field in each individual row of elements.

So each field dynamically inserted will have the same fields, i just need to calculate the fields within that row only.

Here is the jsfiddle for it http://jsfiddle.net/glennmartin/3TnyR/

var LabourItems = {
   rate: null,
   hours: null,
   total: null,
   init: function(object) {
      var rate = parseInt($(object).children('.rate').val(), 10);
      // var rate = $(object).children('.rate').first();
      var hours =parseInt($(object).children('.hours').val(), 10);
      this.total = Number(rate.val()) * Number(hours.val());
      this.updateTotal(object);
   },
   updateTotal: function(object) {
      $(object).children('.total').first().attr('value', this.total)
   }
}

//reactTochange for those inputs that you want to observe
$('.hours').on("click", function() {
   jQuery.each($('.labouritems'), function(key,value){
      LabourItems.init(value);
   });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:24:54+00:00Added an answer on June 13, 2026 at 8:24 am

    Here is your code working:

    var LabourItems = {
       rate: null,
       hours: null,
       total: null,
       init: function(object) {
          this.rate = parseInt($(object).children('.rate').first().val(), 10);
          // var rate = $(object).children('.rate').first();
          this.hours =parseInt($(object).children('.hours').first().val(), 10);
          this.total = this.rate * this.hours;
          this.updateTotal(object);
       },
       updateTotal: function(object) {
          $(object).children('.total').first().val(this.total || 0);
       }
    }
    
    //reactTochange for those inputs that you want to observe
    $('.labouritems input').on("keyup", function() {
       $('.labouritems').each( function(key,value){
          LabourItems.init(this);
       });
    });
    

    Here’s some comments about the changes I’ve made:

    • In your init function you were using var to assing to variables that you’ve declared in the context of your object, so I’ve replaced var rate and var hours with this.rate and this.hours
    • In your $(object).children('.rate') and $(object).children('.hours') calls you needed to use .first() function to get the first element selected. This is because you are selecting by a class name which there may be more than one of (even if there isn’t you’ll find you’ll still be returned with an array)
    • $(object).children('.total').first().val(this.total || 0);: You can use val() to assign just like you have used it to retrieve the value, elsewhere. this.total returns NaN when the textbox is empty so || 0 ensures that if NaN ever appears (it is falsy) then 0 will be used instead (remove it and see what happens if you’re curious)
    • Made sense that you’d want changes on a keyboard event so I’ve made your code use the keydown event. Of course, you can change this to anything you like.
    • When assigning to this.total you were using Number() which was unnecessary because those values have already been passed through parseInt()
    • In the context of the each() function called from a $() DOM selector this provides the element in this iteration. So LabourItems.init(this); passes the actual element to init which is then used via $(object)

    See http://jsfiddle.net/3TnyR/1/

    Well, that’s pretty much it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the below code which is attempting to modify each row in a
I have the below code which returns the total number of pages in my
I have written the below code which has a text-field and a button. as
I have the below code which checks my dynamically produced offhire boxes to see
I have a textbox below which is created through this code: $('#txtWeight').each( function() {
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
I have below snippet of code in which TestClass is extending jPanel which is
I want to know is below code correct ? I have following code which
I have the code below which I use clone() and live() . The code
I have the code below which I use clone() and delegate() . The code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.