Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get userinput in JavaScript [closed]

I want to get user inputs using the tag in HTML and not the prompt function in JavaScript.

I also don't want to have to use another language on top of this like php.

Edit Something like this would be desirable

<form action="demo_form.asp">
  First name: <input type="text" name="fname"><br>
  <input type="submit" value="Submit">
</form>

but without using a .asp

like image 208
Fuzzyketchup Avatar asked Apr 10 '14 03:04

Fuzzyketchup


People also ask

Can you get user input in JavaScript?

In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.

Can JavaScript take input from console?

However, what is not understandable is using JavaScript which runs only the browser, to get input from a systems command line. Doing this is possible using NodeJS. It will require us to use the readline() package. With this package, we can then accept user input and even output it to the console.

How do you get user input in HTML?

The <form> tag is used to get user input, by adding the form elements. Different types of form elements include text input, radio button input, submit button, etc.


2 Answers

Create an input tag with id attribute

<input type="text" id="a" name="b" />

in javascript, you can get the value of an input field via its id

var x = document.getElementById('a').value;
like image 165
Rupam Avatar answered Oct 05 '22 19:10

Rupam


<input type="text" id="input1" />
<button onclick="myJsFunction()"></button>
<script type="text/javascript">
 function myJsFunction(){
    var text=document.getElementById('input1').value;
 }
</script>
like image 39
unconnected Avatar answered Oct 05 '22 18:10

unconnected