DEV Community

Cover image for how to store button state from onclick event
Lalit K
Lalit K

Posted on • Updated on

how to store button state from onclick event

Hello. I am building a shooting game for learning javascript and jquery. Following are tasks to complete to work

  1. On the screen, there should be a button to start the game.
  2. Once the game finishes, it should update the current score.
  3. When a player wins 3 games, show the winner of the match.

I have done the first part and able to show the current state. But, unable to store the previous state and associate with the current. Moreover, last step is not done yet.

Any help is highly appreciated.
Thanks

Discussion (5)

Collapse
benjioe profile image
Benjioe • Edited

You can store it :

  • in a global variable, a data-attributes or an input (type="hidden") but reloading the page will reset score.
  • in local storage or in a cookie but it's harder and cleaning browser's history will reset score.
  • in the URL with history.pushState but it's harder and the player have to remember the URL to keep score.
  • on a server who's the only way to prevent cheating, but you need a server and it's harder.

Or

  • do not store state, just rebind, but it's harder and reloading the page will reset score.
Collapse
benjioe profile image
Benjioe

Can you show us your code ? (put it in your post, on a github repo or with codesandbox)

Collapse
lalitk98 profile image
Lalit K Author

First of all thanks. I am providing my link to js fiddle. Kindly review the code snippet.

Collapse
benjioe profile image
Benjioe
Thread Thread
lalitk98 profile image
Lalit K Author

Thanks Ferrier for this support.