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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:28:36+00:00 2026-05-26T05:28:36+00:00

I have a button, Add to Cart which sends an ajax request when clicked.

  • 0

I have a button, “Add to Cart” which sends an ajax request when clicked. After the request returns successfully, the the button is replaced by “In Your Cart”. “In Your Cart” has a mouseover state: “Remove From Cart”.

The design requirement is that “Remove From Cart” only appear when the user physically moves the mouse over “In Your Cart”. If they simply click “Add to Cart” and don’t move the mouse, the button should say “In Your Cart” after the ajax call completes, even if the mouse is still over the element.

A simple mouseover event listener doesn’t work, because it triggers when the element becomes visible. I’m considering counting mouseovers and mouseouts of a wrapper element, in order to determine if the mouseover event is “real”, or just the result of the element becoming visible, but that’s really ugly. Any other ideas for me?

  • 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-26T05:28:37+00:00Added an answer on May 26, 2026 at 5:28 am

    You could do something like this (edit as appropriate for your AJAX call):

    HTML:

    <div class="cart">
        <button class="add-cart">Add to Cart</button>
        <button class="in-cart" style="display:none;">In Your Cart</button>
    </div>
    

    Javascript:

    var $addCart = $('.add-cart');
    var $inCart = $('.in-cart');
    
    $addCart.click( function(e){
        $addCart.hide();
        $inCart.show().addClass( 'initial' );
    });
    
    $inCart.mouseover( function(){
        if( ! $inCart.is( '.initial' ) ){
            $inCart.text( 'Remove from Cart' );
        }
    });
    
    $inCart.mouseout( function(){
        $inCart.text( 'In Your Cart' ).removeClass( 'initial' );
    });
    

    jsFiddle

    UPDATE

    Based on OP’s comment below, I’ve update the HTML and Javascript as follows:

    <span class='cart'>
        <button class="add-cart">Add to Cart</button>
        <button class="in-cart" style="display:none;">In Your Cart</button>
    </span>
    

    Javascript:

    var $cart = $('.cart');
    var $addCart = $('.add-cart');
    var $inCart = $('.in-cart');
    
    $addCart.click( function(e){
        $addCart.attr('disabled','disabled');
        setTimeout(function(){
            $addCart.hide();
            $inCart.show();
        }, 1000);
    });
    
    $cart.mouseenter( function(){
        if( $inCart.is(':visible') ){
            $inCart.text( 'Remove from Cart' );
        }
    });
    
    $cart.mouseleave( function(){
        if( $inCart.is(':visible') ){
            $inCart.text( 'In Your Cart' );
        }
    });
    

    The differences here are:

    1. The add-cart button is disabled while AJAX is simulated and then hidden.
    2. mouseover and mouseout have been replaced with mouseenter and mouseleave.
    3. These events are now tied to the span wrapper so that the user mouse behavior can be tracked better since the span never hides itself.

    Updated jsFiddle

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

Sidebar

Related Questions

I have a delete button on my web site which I want to add
I have this solution for a single button: myButton.Attributes.Add(onclick, this.disabled=true; + GetPostBackEventReference(myButton).ToString()); Which works
I have a product page with an add to cart button When the add
I have the following button : <%= button_to 'Add to Cart', line_items_path(:product_id => product),
I have one command button for task add to cart, it means when items
I have two validation groups: parent and child I have an add button that
I've got a gridview that renders as a table. I have an Add button
I have a button that, when pressed, will add a new HTML row. Within
Here's my case: I have a table view showing contacts. Add button in the
I have a image button. I wanted to add a text Search on it.

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.