+ 2
How can i get users input and use it again in JavaScript and/or HTML 5?
i want to read a character and store it in a variable to compute it later...
4 Answers
+ 2
Oh, Well Here let me help you :D
So Let me explain a bit, In order to do this You need to give the input an ID, for Example
,
<input type=âtextâ placeholder=âEnter your name..â id=âUsernameâ />
As you can see we gave the Input an ID this allows us to go into JavaScript and use the
document.getElementById(id)
The document.getElementById(id) takes the ID you give it and does whatever you tell it to do. Now please note you can only use document.getElementById(id) in functions it wouldnât work if it was outside a function
In this case we want to use document.getElementById(id) to store the value the player gives us. We have the input an ID called âUsernameâ and weâre going to do .value like this
Example:
function getUsername(){
document.getElementById(âUsernameâ).value ;
}
After you have that you want to store that inside a variable EXAMPLE :
function getUsername(){
var userName = document.getElementById(âUsernameâ).value ;
}
So whenever you use the Variable userName itâs going to give you the users Input inside the Input Text, I hope this helps you if you have any questions Feel Free to ask me
+ 4
var c = prompt()[0];
You don't necessarily need the [0] I put it there just so if the user enters more than just one character it will automatically use the first character.
+ 3
Here is some code I wrote that allows the user to enter a string into an imput and then pulls the value when a button is clicked using document.getElementById ("id" ) ;
This should help
https://code.sololearn.com/W49467g9No21/?ref=app
+ 3
OK thanks guys