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

  • SEARCH
  • Home
  • 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 7565241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:06:15+00:00 2026-05-30T14:06:15+00:00

I don’t think this is a Drupal-specific question, but more of a general jquery/ajax

  • 0

I don’t think this is a Drupal-specific question, but more of a general jquery/ajax issue:

Basically, I’m trying to use javascript to add up form fields and display the result in a “subtotal” field within the same form. Everything is working fine until i click the option to add another field (via ajax), which then changes my “subtotal” field to zero, and won’t work again until I remove the field.

Here is the function that adds up the fields:

function calculateInvoiceFields(){
var total = 0;
var rate = 0;
var quantity = 0;
var i = 0;
var $ = jQuery;

    $("#field-aminvoice-data-values tr").each(function(){
        // quantity field number
        quantity = $("#edit-field-aminvoice-data-und-"+i+"-field-aminvoice-quantity-und-0-value").val();
        // rate field as number
        rate = $("#edit-field-aminvoice-data-und-"+i+"-field-aminvoice-rate-und-0-value").val();
        if(!isNaN(quantity) && !isNaN(rate)){
            total  += quantity*rate;
        }
        i++;
    });

return total;   
}

And here are the functions that get fired for .ready and .live:

jQuery(document).ready(function(){
    var $ = jQuery;
    $(".field-type-commerce-price input").val(calculateInvoiceFields());
});


jQuery(function(){
    var $ = jQuery;
    $(".form-text").live('change', function(){
        $(".field-type-commerce-price input").val(calculateInvoiceFields());
    });
});

Any ideas would be a big help. Thanks in advance!

  • 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-05-30T14:06:16+00:00Added an answer on May 30, 2026 at 2:06 pm

    I recommend using ‘on’ for any binding statement. and ‘off’ for unbinding.
    The reason it doesn’t work after an AJAX call, is because you need to be watching for that element to be added to the DOM, and an event attached to it after it gets loaded. If you load a new element in, and there is nothing watching for it, it won’t add the event watch to that new DOM element.
    As below:

    function calculateInvoiceFields(){
       /*..*/
    return total;   
    }
    
    
    $(document).ready(function(){
        $(".field-type-commerce-price input").val(calculateInvoiceFields());
        $("body").on('change', ".form-text", function(){
            $(".field-type-commerce-price input").val(calculateInvoiceFields());
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if I worded the question right, but basically what I want to
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if I'm over-thinking this or not.. but I'm trying to be able
don't know if the title describes anything about what I'm trying to say but
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I don't know why, but this code worked for me a month ago... maybe
Don't know if this is the right place to ask this, but I will
Don't think my virtualhost is working correctly. This is what I have inside of
Don't know why this is happening, but after submitting a form via JS (using
This could be a duplicate question, but I have no idea what search terms

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.