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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:02:03+00:00 2026-05-14T07:02:03+00:00

I am trying to build a shopping website with ajax. When a user clicks

  • 0

I am trying to build a shopping website with ajax. When a user clicks the “add to cart” image. The little loading image will show next to the Add To Cart Image. The first click works fine and the image showed as I expected. However, the second and the following clicks appends more images on the first loading image(2nd:add two loading images, 3rd: add three images..6 total images after 3 clicks). I did use ajaxStop and remove the first image…Not sure what’s going on…Could use a help. Thanks a lot.

My javascript code

// add to cart
$(".addToCart").click(function(e){
 $this=$(this);
 $tableId=$this.closest('table').attr('id');


$($this).prev().ajaxStart(function(){
   $("<img class='loader' src='images/loader.gif'>").insertBefore($this);
});


$($this).prev().ajaxStop(function(){
   $($this).prev().remove(); 
});

HTML

<table>
<tr>
   <td width="146" align="right" valign="middle">
<br>
<span id="wasPrice"><?php echo $productPriceWas; ?></span>
<br>

<?php echo "$".$productPrice;?><br>**//I want my image here**<a class="addToCart" href="javascript:void(0);"><img src="images/addToCart.gif" alt="add To Cart"/><a/>     </td>
        </tr>
         </table>
  • 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-14T07:02:03+00:00Added an answer on May 14, 2026 at 7:02 am

    .ajaxStart() is a global event. Each click you are binding another set of event handlers which causes 2 or more loading images to show up. You could try using the .one(types, function() { ... }) event binding to only fire that block of code once per click.

    However, I would suggest looking at the $.ajax() callbacks beforeSend and complete as places to bind code for a specific ajax request (as opposed to every ajax request).

    I usually use a pattern something like this:

    $(".addToCart").click(function(e){
    
       // you need to use "var" to make sure $this is only available inside this function
       var $this=$(this);
    
       // avoid using $ on variables that aren't jQuery objects (this is just a string)
       var tableId=$this.closest('table').attr('id');
    
       // insert the element before starting the ajax call
       var $loadingElem = $("<img class='loader' src='images/loader.gif'>");
       $loadingElem.insertBefore($this);
    
       // call ajax with some data
       $.ajax({
         url: '...', data: {id: tableId}, // ....
         complete: function(xhr, textStatus) {
           // remove the element when the ajax completes
           $loadingElem.remove();
         }
       });
     });
    

    Also, $($this) is a wasted call, it will just return $this

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

Sidebar

Related Questions

I am trying to build a shopping cart site. When a user click add
I am trying to build a simple shopping cart, i read about controlling them
I'm trying to build a shopping cart. There's very simple class which creates a
Trying to build a shopping cart based on the Agile Web Development 4th edition.
I'm currently trying to build a personal website to create a presence on the
Hi i was recently looking over a shopping cart paginator class, trying to understand
I'm trying build a regex that will replace any characters not of the format:
Trying to build a plugin effect that will some what look better than this
Trying to build a dynamic gallery that is loaded via AJAX. I'm trying to
Trying to build my own user control: <%@ Control Language=C# AutoEventWireup=true CodeFile=TabMenu.ascx.cs Inherits=TabMenu %>

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.