[#Solved]Change a value everytime the Button is clicked.
I was trying to make a code like:
1. When the button is clicked for the 1st time, it will print 0.
2. When it's clicked 2nd time, it will print 1.
3. When it's clicked 3rd time, it will print 2.
4. When it's clicked for the 4th time, again the value will start from 0.
5. When it's clicked for the 5th time, it will print 1.
6. The 7th time, it will again start from 0.
I think i've told my question clearly. Pls give the solution ❤❤❤
I've tried, here is the code:
Thanks a lot Russ for telling "You don't need a for loop, it's much easier" and Vignesh Rathnam for using "onload" event in ur code, these helped me a lot. Here is the solution to my question :
Are u talking about my code 👑Mr_Gamer👑? Then pls see my solution code again, it returns. And if u are talking about Vignesh Rathnam 's code, well,,, he just told about the structure(so it doesn't need to return anything). Solving is my job.
Your first issue is that you're getting your button using getElementsByTagName which returns an array. This means you either have to do x.onclick = ... or add an id to the button and get it using getElemenyById.
Also, I'd suggest adding an extra div or something to print your number to when you press the button.
An extra hint: within your onclick() function, you don't need any for loops. It is much simplet than that.
No. You only have 1 button. getElementsByTagName("button") will return an array of all buttons on your page. As you only have 1 button, x will be your only button. I'd advise giving it an id and getting it using getElementById to make it all tidier.
This is what I mean: https://code.sololearn.com/WRM6BwHK6iPy/?ref=app