• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Click Button


New Coder

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.

Last edited by a moderator:
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.
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.

New Threads

Latest posts

Buy us a coffee!

Top Bottom