Let's say that I have the following three buttons in a div on a page:
<div class="action_buttons">
<button class="btn btn-sm btn-outline-primary add_to_cart">Add to Cart</button>
<button style="display:none;" class="btn btn-sm text-success added"><i class="fas fa-fw fa-check"></i> Added</button>
<button style="display:none;" class="btn btn-sm text-danger remove"><i class="fas fa-fw fa-times"></i> Remove</button>
</div>
What I'm tying to achieve is the following:
- When button with class
add_to_cartis clicked, hide it and show button with classadded - When button with class
addedis hovered on, hide it and show button with classremove - When button with class
removeis hovered on, hide it and show back the button with classadded - When button with class
removeis clicked, hide it and show back button with classadd_to_cart
So far this is the code I have:
$(document).on('click', '.add_to_cart', function(e){
e.preventDefault();
$(this).hide();
$(this).closest('div').find('.added').show();
});
$(document).on('mouseenter', '.added', function(e){
$(this).hide();
$(this).closest('div').find('.remove').show();
});
$(document).on('mouseleave', '.remove', function(e){
$(this).hide();
$(this).closest('div').find('.added').show();
});
$(document).on('click', '.remove', function(e){
e.preventDefault();
$(this).hide();
$(this).closest('div').find('.add_to_cart').show();
});
The issue that I have with this code is that when I click on Remove, because there is a mouseleave event on that button that shows the button with class added, that button is still visible after I click remove. I want only the button with class add_to_cart to be visible when I click remove.
Thanks in advance for any help.