How to set value of a variable on button click | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

How to set value of a variable on button click

like in a calculator when u tap 1 it shows 1 but the only exception is that I need only 4 digits and I need to assign each digit to each variable, like if first digit is 1(or any other no.) then its variable should be num1 and for second digit num2 , third digit num3 and lastly fourth digit num4 can anybody help?

9th May 2017, 5:54 PM
_Retr0/-
_Retr0/- - avatar
35 Answers
+ 3
what about this. <button onclick="assign(1);" >num1</button> <button onclick="assign(2);" >num2</button> in JavaScript var x=0; var arrs = []; function assign(val){ arrs[x] = val; x++; } now each array is new val;
9th May 2017, 6:12 PM
MR Programmer
MR Programmer - avatar
+ 7
its not that simple
9th May 2017, 6:01 PM
_Retr0/-
_Retr0/- - avatar
+ 7
by your function I can set the variable of num1 what about num2
9th May 2017, 6:02 PM
_Retr0/-
_Retr0/- - avatar
+ 7
won't help it either
9th May 2017, 6:05 PM
_Retr0/-
_Retr0/- - avatar
+ 7
what if user wants to assign 2 as num1 we don't know I guess we should use if-elseif-else here
9th May 2017, 6:06 PM
_Retr0/-
_Retr0/- - avatar
+ 7
no no not actually a calculator just give me that part of code rest would be done by me
9th May 2017, 6:08 PM
_Retr0/-
_Retr0/- - avatar
+ 7
its a code cracker game
9th May 2017, 6:09 PM
_Retr0/-
_Retr0/- - avatar
+ 7
The user has to guess the no. through a series of hints
9th May 2017, 6:10 PM
_Retr0/-
_Retr0/- - avatar
+ 7
here is half coded version by me https://code.sololearn.com/WW7ViFM55ft0/?ref=app BTW I know that assign function is not working
9th May 2017, 6:11 PM
_Retr0/-
_Retr0/- - avatar
+ 7
alright I will try it
9th May 2017, 6:13 PM
_Retr0/-
_Retr0/- - avatar
+ 7
still not working :'(
9th May 2017, 6:18 PM
_Retr0/-
_Retr0/- - avatar
+ 7
yeah that's the problem
9th May 2017, 6:21 PM
_Retr0/-
_Retr0/- - avatar
+ 7
could you specify?
9th May 2017, 6:22 PM
_Retr0/-
_Retr0/- - avatar
+ 7
so do I take it out of it?
9th May 2017, 6:24 PM
_Retr0/-
_Retr0/- - avatar
+ 7
BTW every time I add the window. onload function my every web code doesn't work idk y?
9th May 2017, 6:27 PM
_Retr0/-
_Retr0/- - avatar
+ 7
so where should be the window onload function?
9th May 2017, 6:29 PM
_Retr0/-
_Retr0/- - avatar
+ 7
okay
9th May 2017, 6:31 PM
_Retr0/-
_Retr0/- - avatar
+ 7
Thx again
9th May 2017, 6:31 PM
_Retr0/-
_Retr0/- - avatar
+ 6
Thx it works now
9th May 2017, 6:26 PM
_Retr0/-
_Retr0/- - avatar
+ 2
no window load is correct but you should not have function inside it.
9th May 2017, 6:30 PM
MR Programmer
MR Programmer - avatar