0

I want a counter to display a button for each new visitor that when clicked once will increment the display BUT only allow a single click per visitor.

Like this but with a the button being disabled after one click https://stackoverflow.com/a/13328728/4307913

Fiddle Link

<script>
var x = 0;
var already = false;
    function cnt() {
        if(!already) {
                document.getElementById("num").innerHTML = x+=1;
                already = true;
        }
    }
</script>

<div id="counter">
    <h2> support counter </h2>
    <div onclick="cnt()">
         <span id="num">Click here to show support</span>
    </div>
</div>

I found this link: https://stackoverflow.com/a/12651098/4307913 is this mandatory?

1
  • 1
    You want each visitor to see how many support-clicks have been made previously by other people as well? If that's the case, you are going to need server side persistance to keep track of the clicks. Commented Dec 28, 2014 at 11:35

1 Answer 1

1

Simplest way would be to store that the user has clicked in localStorage otherwise if that's not good enough (which it usually isn't) then you would need to implement either cookie tracking or keep track of it on the server side of things.

function cnt () {
   if (!JSON.parse(localStorage.getItem('hasClicked'))) {
       document.getElementById("num").innerHTML = x+=1;
       localStorage.setItem('hasClicked', true);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.