I am doing a shopping cart website and I want my Add to Cart button to say Added item by clicking on it, but only 2 seconds after that it changes back to Add to Cart . How do I achieve this?
Try the following:
$('button.add').click(function() { var self = $(this); if (!self.data('add')) { self.data('add', true); self.text('Item added'); setTimeout(function() { self.text('Add to Cart').data('add', false); }, 2000); } });
In simple Javascript, you can use the variable to test the click of a button, and if not, set the button to the desired line and change it after two seconds.
document.getElementById('button').addEventListener('click', function (clicked) { return function () { if (!clicked) { var last = this.innerHTML; this.innerHTML = 'Item added'; clicked = true; setTimeout(function () { this.innerHTML = last; clicked = false; }.bind(this), 2000); } }; }(false), this);
<button id="button">Add to Cart</button>
After pressing the button, the button text will be changed and the button will also be disabled to prevent further actions, and then return back after two seconds.
(function() { $(".btn-addcart").on("click", function() { var $this = $(this), oldText = $this.text(); $this.text("Item added"); $this.attr("disabled", "disabled"); setTimeout(function() { $this.text(oldText); $this.removeAttr("disabled"); }, 2000); }); })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <button class="btn-addcart">Add to cart</button>
<input type="button" id="btn"/> $("#btn").click(function(){ $(this).val("Item Added"); setTimeout(function(){ $("#btn").val("Add to Cart"); }, 2000); });
Source: https://habr.com/ru/post/1655294/More articles:Cannot start project application (Gradle build failed) - androidhow to iterate over an array of mail objects received through Wp Rest Api in js reaction - javascriptHow to distinguish from parent to chlid class interface - c ++Найти несколько элементов в веб-представлении с эспрессо - androidAndroid RecyclerView Endless Scroll adds throwing Exception item for Android Support Library 24.2.1 - androidWhat does N '@date mean? - sqlDeviceControllerApplicationConnectionFailed on google chromecast - ioshttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1655297/does-a-commit-in-git-represent-the-state-of-a-repository&usg=ALkJrhgYzOQmCvfFYzbBupy9KU9G4jVU-ADrawing an abstract UML class on paper without italics? - javaDynamic playlists with Exoplayer 2 - androidAll Articles