How can I save user input as a variable in html/JavaScript?


#1

I’m trying to make an html file that asks the user for three numbers then puts the input in separate variables to be used with the code… If anyone knows how please tell me…


#2

did you want something like

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>prompt demo</title>
  </head>
  <body>
    <h1 id='demo'> </h1>
    <script>
      var name=prompt('your name please')
      document.getElementById("demo").innerHTML='your name is '+name
    </script>
  </body>
</html>

#3

Sorry no I meant to change what I said… What I meant was the user inputs three numbers and it gets put into separate variables to be used in the code… Sorry about that


#4

It’s just the example how you can do that :slight_smile:

so same way you can create three variable and ask users for input.

can i see your code what you’ve done so far?


#5

Ok thanks I will try that and I can’t show you my code currently… But I will when I get the chance.:smiley:


#6

It worked Thank You:smiley:


#7

I m so happy that you solved it :slight_smile:


#8

Wait would this be possible in a type of input?


#9

<. Input. Type. =. “text”. >


#10

yes, using <input type='text'> is absolutely possible.


#11

How would I do it the code you gave me did not work


#12

you still would need to write the code to make it work, your question was if it was possible. I confirmed that the use of <input type='text'> is possible

well, we would need to make a button to allow the user to submit there entered text, then we would need either onclick attribute on the <input type='button'> to call a javascript function, or add an event listener to the button

once we done this, we can retrieve the input from the field.

This is a lot more complicated then using the prompt() function as you can see

we could use var input = document.getElementById("input_id").value; to get the value of input field, where input_id is the id of your input field.