Skip to content
Advertisement

Start/Stop button not working as expected

I have recently started learning JavaScript, and I’d like to make a button, with a function, which changes the innerHTML on click. On the first click it changes the text, but after that nothing. Any ideas how could I fix that? Here is the code so far:

JavaScript
JavaScript

Advertisement

Answer

You have to set the turnedOn flag outside of the click method, otherwise it will always be false on click.

Also you’re setting the flag turnedOn inside the if-else statement in a reversed way.

Note: As you are only changing the text, you can use textContent

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement