I am looking to customize a button element to function as an 'add to bag' button with multiple text states triggered by user clicks. Initially, the button will display "Add to Bag," then change to a spinner image upon click. After a 1-second animation in this state, the text should switch to "Added to Bag!" before returning to the original "Add to Bag" state.
For reference, you can see a similar functionality in action on this page:
Below is a snippet of code that accomplishes most of these text changes, except for the spinner state:
// Cart button text change
(function() {
$(".btn-cart").on("click", function() {
var $this = $(this),
oldText = $this.text();
$this.text("Added to Bag!");
$this.attr("disabled", "disabled");
setTimeout(function() {
$this.text(oldText);
$this.removeAttr("disabled");
}, 1600);
});
})();