JavaScript Need help Click Button

CncMS

New Coder
Aug 8, 2019
1
0
1
Hi,

I have this code:

Captura de ecrã 2019-08-08, às 17.26.26.png

which makes me have a button (Click Me) where every time I click it, the number changes upwards (from 1 to 99). Whenever it reaches 99, another button appears (Reset Me) that makes the count start again. - This makes it necessary to have two buttons.

What I would like to understand was: is it possible to put everything in one button? I believe it's a javascript function that I don't know about.

Thanks,
 
Last edited by a moderator:

eeflores

Coder
Jun 26, 2019
58
17
8
I've copied the code into a pen: https://codepen.io/eeflores/pen/NWKxzba . I'm assuming that there's an extra library being used: KnockoutJS based on references to ko in the javascript. I'll see if I can look more into it later, but pressing the button doesn't seem to increment the counter. I'll have to figure stuff out, but maybe someone with more experience may be able to quickly see a solution.

The code does seem to imply that the main click button will be disabled once the counter reaches a certain value, which is probably not desirable if only one button were to be used. It seems that a simple check for range can be made to reset the value back to 0 once the max value has been reached or exceeded.
 
  • Like
Reactions: Malcolm

Ghost

Active Coder
Moderator
Apr 19, 2019
326
149
43
It's using Knockout.js, so you must make the following changes:
  1. Remove the disable data bind from click button
  2. In the click func: if count >=99, remove current data-bind of click button and add the data-bind for reset on to button. Change button text to say Reset... you can change classes too if needed.
  3. Alter the reset func to once again replace the data-binds and alter the button text.