0

I've got this code to display a different button once the other button has been clicked.

<a href="#" id="ac-button-top" class="btn btn-success add-to-cart">order</a>
<a href="#" id="ac-button-bottom" class="btn btn-success">test</a>
$('.add-to-cart').click(function() {
  $('#ac-button-top').css("display", "none");
  $('#ac-button-bottom').css("display", "block");
});

This works fine, however I need to save this click event somehow so even when the page is refreshed it still displays the second button and not the first one. My guess is you would do this using ajax but I'm a newbie to ajax and couldn't find any solution to this when I searched so if anybody could point me in the right direction of how to do this? Or any other way not using ajax?

3
  • 1
    If you don't need to send it to a server you don't need ajax, use localStorage. Commented Oct 5, 2017 at 9:59
  • AJAX would work if you want to save state in a server side data store. Cookies, localStorage or sessionStorage would work just as well if you want to keep it simple without getting a server involved. Commented Oct 5, 2017 at 9:59
  • 1
    use localStorage Commented Oct 5, 2017 at 10:00

3 Answers 3

2

You can use localStorage. This is fiddle https://jsfiddle.net/y0ymqps7/2/

HTML

<a href="#" id="ac-button-top" class="btn btn-success add-to-cart">order</a>
<a href="#" id="ac-button-bottom" class="btn btn-success">test</a>

Javascript

$('.add-to-cart').click(function() {
  $('#ac-button-top').css("display", "none");
  $('#ac-button-bottom').css("display", "block");
  localStorage.setItem("clicked", 1);
});
$( document ).ready(function() {
    if(localStorage.getItem("clicked") == 1){
      $('#ac-button-top').css("display", "none");
      $('#ac-button-bottom').css("display", "block");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Use either localStorage or sessionStorage. The difference between them is localStorage has no expiration date set, while sessionStorage is cleared when page session ends i.e. browser window or tab is closed. Page session survives page reloads and restores.

You can think of something like:

$('.add-to-cart').click(function() {
  $('#ac-button-top').css("display", "none");
  $('#ac-button-bottom').css("display", "block");
  window.sessionStorage.setItem('addToCartClicked', true);
});

// some operation later...
if (window.sessionStorage.getItem('addToCartClicked') == 'true') {
  ...
}

Comments

1

If you need to save click event count or datas. If you need to save these details from database. then only you can go with ajax method. Otherwise you can go with other method like..

use sessionStorage or localStorage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.