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 8032463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:23:35+00:00 2026-06-05T01:23:35+00:00

I have a simple .change function that goes to the database and gets some

  • 0

I have a simple .change function that goes to the database and gets some values sets some values, ect.

$('.linequantity').change(function() {
    ...
    $.getJSON(url, function(data) {
      $('#TT-' + lineItem).text(parseFloat(data['price']) * quantity);
    });
});

What I want to then, is on any of the linequantites changing, I want to update the subtotal. (Yes it’s a catalog type/shopping cart style app). My thought was, lets use a .each

$('.linequantity').each(function(){
     ...
   var url = "jQueryFunctions.php?action=getPrice&quantity=" + quantity + "&customer=" + customerID + "&item=" + lineItem;

    $.getJSON(url, function(data) {
       subtotal = subtotal + (parseFloat(data['price']) * quantity);
     });
   });

   $('#subtotal').text(subtotal);

when I do this though, I get 0 for the subtotal, if the each even fires at all.

I have tired putting the .each in a function. When I moved the .each into it’s own function it fired more consistently, but the getJSON, which in the .change works just fine gets skipped.

Am I running into a conflict of the first json request isn’t quite complete before I ask for another one?

I am open to a change in methodology, but thing the above has the right idea/start to it.

Any ideas?

EDITS
quantity field

<input type="text" class="linequantity" id="SQ-<?php echo $value['itemNumber']; ?>" name="Quantity" placeholder="#"/>

total span

<span id="subtotal" class="currencyField">$0.00</span>

11:44 EDIT:
I think the .each is hitting before the getJson completes. How can I make sure the json request completed before I do not jQuery

  • 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-05T01:23:38+00:00Added an answer on June 5, 2026 at 1:23 am

    I think you just have a race condition whereby the ajax requests are completing too late. The only way to get around this is to have methods that depend on the requests run after they complete. There are a lot of ways to do this such as using Deferred and $.when. The simple way would be nice:

    $.getJSON(...)
       .done(function () {
          // Stuff that depends on JSON goes here
       });
    

    In the .each you are depending on multiple requests — possibly a lot at a time. I would try to do this in one request (e.g. update your server code to accept a list of items and compute the subtotal instead of just having them one at a time). Otherwise, you have to update the subtotal continuously, which may cause it to flicker visually. It’s also inefficient for the server. You may be able to do something like:

    var jqXHRs = []
    $.each(...
       jqXHRs.append($.getJSON(...));
    );
    $.when(jqXHRs).done(...);
    

    …but I’m not sure whether $.when will work with an array like that, or if there is any way to expand the argument list to it.

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

Sidebar

Related Questions

I have a really simple function that should: change the wording (from expand to
I have a function that calculates the left and right node values for some
I have a simple function that counts from 0 to 5000 and does funky
So I have an extremely simple $.post function that works perfectly in all the
Let's say I have a simple class called WebsterDictionary that has a function that
I have simple piece of code to upload files: $(document).ready(function () { $(.attachmentsUpload input.file).change(function
I have this very simple br2nl function that I use to take a string
i have pretty simple simple question (i hope so). How do i change the
so I have a simple problem. I have a hover background image change effect,
I have a simple UIView animation block. In the block, I only change the

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.