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

25th Mar 2018, 9:06 PM
Fotie
Fotie - avatar
4 Réponses
+ 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
26th Mar 2018, 2:17 AM
AClue
AClue - avatar
+ 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.
25th Mar 2018, 9:15 PM
Kevin Eldurson
Kevin Eldurson - avatar
+ 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
25th Mar 2018, 9:15 PM
josh mizzi
josh mizzi - avatar
+ 3
OK thanks guys
25th Mar 2018, 9:36 PM
Fotie
Fotie - avatar