+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

20 Answers

New Answer

+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

+4

Russ my code is given :) Pls see again

+4

Russ will i need to use x[i].onclick in a for loop?, as it have an "i" with it?

+2

👑Mr_Gamer👑 what ain't?

+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.

+1

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.

+1

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

+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) 😁

+1

Ipang exactly that. The link to the code was right there, but I don't know what was going on in my brain.

+1

Russ It happens I guess, but no need to punish yourself over that 😁

+1

It's not as if it really matters though. It's only internet points 😊

+1

You are right Russ 👍

+1

You need to load DOM(which is another page) (document elements ) so they wont be null https://code.sololearn.com/WuMn5F4U8dn8/?ref=app

0

At least show us what you've already tried. Edit: downvoted myself because I'm an idiot.

0

🤦‍♂️ wow, it's one of those days! Sorry!

0

Welcome Raihan🇧🇩 [Thunder_Rabbit⚡⚡]

0

👀Nah It Ain't 💔

0

It Ain't Giving Out A Value👀💔

0

0

😁