JavaScript has no native means of getting input. Every method that exists is an extension provided by the host environment (prompt is an example of that)
Different host environments have different methods. You've identified one for browsers, DOM is another. NodeJS has the Readline API (among others). There are lots of other environments (WSH, JXA, etc, etc).
Answer from Quentin on Stack OverflowJavaScript has no native means of getting input. Every method that exists is an extension provided by the host environment (prompt is an example of that)
Different host environments have different methods. You've identified one for browsers, DOM is another. NodeJS has the Readline API (among others). There are lots of other environments (WSH, JXA, etc, etc).
I don't know about a specific command to get it out in the prompt but maybe you can use the alert();
Hi, can javascript implement a function to read input from keyboard, like pascal readln, gwbasic input, C scanf/gets, C++ cin ? It should load an entire sequence of keys (as it was a string) into a variable WHEN RETURN IS HIT
Just to complete what follows:
<head>
<script>function input(){
/* ?????????????? */
}
</script>
</head><body> <tscript> alert("Enter something"); x=input(); alert("You entered: "+x); </script>
when return is hit, input() should load into x the entire sequence of keys ( as a string)
I am an absolute beginner and I just tried to copy and paste some code from some books, unsuccesfully.
Tnx!
user input without prompt window
Getting user input in javascript without using prompt - Stack Overflow
Rather than using prompt, how can we collect input from a user via a form in the HTML...then display results and score?
html - Get userinput in JavaScript - Stack Overflow
Videos
Submit ``` Notice that the input tag has an id of "question1" I'll be targeting that element with Javascripts document.getElementById method which targets elements in the DOM. And also notice the onclick event object binded to the button element. This is used to call a the function submitInfo within our script containing the code for the function. myscript.js ```javascript function submitInfo(){ var question1 = document.getElementById("question1").value; if(question1 == "Apple"){ confirm("You got it!"); } } ``` Above is the Javascript file in which contains my code. Here I placed my submitInfo function which is activated by the button element in my HTML file. As explained earlier, I attached the event object onclick="function call goes here" to the ** button** tag in order to allow the function to be called once the button is clicked. Now lets discuss the Javascript code. I store the users value in the variable question1. The way I obtain the value from the input tag is to target the input tag with the document.getElementById function, and within the function I specify that I want to target the question1 id associated with the input tag. Once targeted I use the value property in Javascript to obtain the text value placed within the input tag. I then use an if statement to evaluate the variable to see if the user got the right answer. I hope this gave you some inspiration on making games without the use of prompts. Have a good day. :)
<input type="text" id="input1" />
<button onclick="myJsFunction()"></button>
<script type="text/javascript">
function myJsFunction(){
var text=document.getElementById('input1').value;
}
</script>
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;
I m just learning JS and I m using the document.write method to display output. I need something equally simple, even if its not "good" coding, to accept keyboard input. The idea is to accept a number or string. Thanks for your time.
Append some more stuff"; }); jQuery: $(document).ready(function(){ // dom ready });
I'm trying to write a code that would ask the user to put in their birth year and give the result of how old Mozart would have been when the user was born. But I don't know how to allow the script to take what the user put in and perform the function on it.
(I know this is super basic, but I've been looking for ages and I can't find the answer anywhere. I'm very new, so I'd really appreciate it if you could use simple words, because things like "initiating" a variable still sound confusing).
Here's what I've got so far:
<body> <p>How old would Mozart have been when you were born?</p>
<form action="" method="get">
<label for="year">Year of birth:</label><br>
<input type="text" id="year" name="year"<br>
<input type="submit" value="Submit">
</form>
<script> let mozart = 1756; let year = 1990; function difference (year, mozart) { let x = year - mozart; return x; } let y = difference (year, mozart); console.log('Mozart would have been ' + y + ' years old when you were born.'); </script>