How to assign a value from <input> to a variable? JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to assign a value from <input> to a variable? JS

How to assign y a value from < input >? Html <input type="text" id="answer"> <button onClick="verifyText" >Verify</button> JS function verifyText () { var x = "It is a text" ; If (x == y) { alert('hello world') } } How can i do y as a text from input How to assign y a value from the input

14th Jul 2019, 12:12 PM
Alex
Alex - avatar
19 Answers
+ 1
like you want the input as int ?
14th Jul 2019, 1:56 PM
Taste
Taste - avatar
+ 1
<input type="number"
14th Jul 2019, 2:21 PM
Taste
Taste - avatar
+ 1
For example, I entered the text "it is a text" And I need to compare this text with the variable x
14th Jul 2019, 2:23 PM
Alex
Alex - avatar
+ 1
line 6, if not If
14th Jul 2019, 2:33 PM
Taste
Taste - avatar
0
y = document.getElementById("answer).value
14th Jul 2019, 12:29 PM
Taste
Taste - avatar
0
And if i have a text there?
14th Jul 2019, 1:54 PM
Alex
Alex - avatar
0
It's not a value
14th Jul 2019, 1:54 PM
Alex
Alex - avatar
0
Yes
14th Jul 2019, 2:18 PM
Alex
Alex - avatar
0
ooooooh use my first example. then if(x==y)
14th Jul 2019, 2:27 PM
Taste
Taste - avatar
0
What's wrong?
14th Jul 2019, 2:34 PM
Alex
Alex - avatar
0
you have a typo in line 6. if should be lowercase
14th Jul 2019, 2:35 PM
Taste
Taste - avatar
0
I entered a text "It is a text" and nothing happened
14th Jul 2019, 2:36 PM
Alex
Alex - avatar
0
Oh yeah
14th Jul 2019, 2:37 PM
Alex
Alex - avatar
0
And now
14th Jul 2019, 2:38 PM
Alex
Alex - avatar
0
line 9 in html you need () to call the function verifyText()
14th Jul 2019, 2:39 PM
Taste
Taste - avatar
0
Oh yeah
14th Jul 2019, 2:45 PM
Alex
Alex - avatar
0
Thank you very much
14th Jul 2019, 2:45 PM
Alex
Alex - avatar
0
sure
14th Jul 2019, 2:45 PM
Taste
Taste - avatar