[#Solved]Change a value everytime the Button is clicked. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

[#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: https://code.sololearn.com/WJtLpmv9dQUf/?ref=app

13th Oct 2019, 2:42 PM
Raihan Arefin🇧🇩 [Thunder_Rabbit⚡⚡]
Raihan Arefin🇧🇩  [Thunder_Rabbit⚡⚡] - avatar
19 Answers
+ 5
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 : https://code.sololearn.com/W75eMF2C8pZl/?ref=app
14th Oct 2019, 3:45 AM
Raihan Arefin🇧🇩 [Thunder_Rabbit⚡⚡]
Raihan Arefin🇧🇩  [Thunder_Rabbit⚡⚡] - avatar
+ 4
Russ my code is given :) Pls see again
13th Oct 2019, 2:54 PM
Raihan Arefin🇧🇩 [Thunder_Rabbit⚡⚡]
Raihan Arefin🇧🇩  [Thunder_Rabbit⚡⚡] - avatar
+ 4
Russ will i need to use x[i].onclick in a for loop?, as it have an "i" with it?
13th Oct 2019, 3:25 PM
Raihan Arefin🇧🇩 [Thunder_Rabbit⚡⚡]
Raihan Arefin🇧🇩  [Thunder_Rabbit⚡⚡] - avatar
+ 3
Your first issue is that you're getting your button using getElementsByTagName which returns an array. This means you either have to do x[0].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.
13th Oct 2019, 3:17 PM
Russ
Russ - avatar
+ 3
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[0] 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
13th Oct 2019, 3:29 PM
Russ
Russ - avatar
+ 2
👑Mr_Gamer👑 what ain't?
14th Oct 2019, 11:42 AM
Raihan Arefin🇧🇩 [Thunder_Rabbit⚡⚡]
Raihan Arefin🇧🇩  [Thunder_Rabbit⚡⚡] - avatar
+ 2
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.
14th Oct 2019, 1:47 PM
Raihan Arefin🇧🇩 [Thunder_Rabbit⚡⚡]
Raihan Arefin🇧🇩  [Thunder_Rabbit⚡⚡] - avatar
+ 1
Russ There's no sin in asking for code in relation with the question, what's with self down voting anyways? did you miss it at first look (been there) 😁
13th Oct 2019, 4:14 PM
Ipang
+ 1
Ipang exactly that. The link to the code was right there, but I don't know what was going on in my brain.
13th Oct 2019, 4:18 PM
Russ
Russ - avatar
+ 1
Russ It happens I guess, but no need to punish yourself over that 😁
13th Oct 2019, 4:20 PM
Ipang
+ 1
It's not as if it really matters though. It's only internet points 😊
13th Oct 2019, 4:24 PM
Russ
Russ - avatar
+ 1
You are right Russ 👍
13th Oct 2019, 4:26 PM
Ipang
+ 1
You need to load DOM(which is another page) (document elements ) so they wont be null https://code.sololearn.com/WuMn5F4U8dn8/?ref=app
13th Oct 2019, 8:40 PM
Vignesh Rathnam
Vignesh Rathnam - avatar
0
At least show us what you've already tried. Edit: downvoted myself because I'm an idiot.
13th Oct 2019, 2:51 PM
Russ
Russ - avatar
0
🤦‍♂️ wow, it's one of those days! Sorry!
13th Oct 2019, 3:10 PM
Russ
Russ - avatar
14th Oct 2019, 6:30 AM
Vignesh Rathnam
Vignesh Rathnam - avatar
0
👀Nah It Ain't 💔
14th Oct 2019, 11:06 AM
Siyabonga Samson Lukhele
Siyabonga Samson Lukhele - avatar
0
It Ain't Giving Out A Value👀💔
14th Oct 2019, 11:50 AM
Siyabonga Samson Lukhele
Siyabonga Samson Lukhele - avatar
0
😁
15th Oct 2019, 9:30 AM
osama