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 Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ javascript input from keyboard without "prompt()" popup (like cin in c++, readln in pascal, input in gwbasic...)
r/javascript on Reddit: javascript input from keyboard without "prompt()" popup (like cin in c++, readln in pascal, input in gwbasic...)
January 16, 2015 -

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!

Discussions

user input without prompt window
In C if I wanted to do this I'd use a gets or scanf statement. Is there an equivelent in JS? I have a loop, and within the cycle I want input from the user. I don't want to use a prompt. I'd prefer it to be mouse driven if possible. Ideally, I'd like it to perform the actions within the... More on tek-tips.com
๐ŸŒ tek-tips.com
2
0
May 14, 2003
Getting user input in javascript without using prompt - Stack Overflow
I have a setup where I allow the user to type commands in a regular html textfield. I handle the onkeypress event on the textfield, check if it is an enter key that has been pressed, and parse and More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 23, 2017
Rather than using prompt, how can we collect input from a user via a form in the HTML...then display results and score?
Chuck Robertson is having issues with: I want to build some very simple games. Quizzes will work...how do I build a function that evaluates user input from an HTML form against a set ... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
2
April 16, 2015
html - Get userinput in JavaScript - Stack Overflow
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 wou... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ how to take input in javascript without prompt
How to take input in JavaScript without prompt
August 23, 2022 - A simple example code gets user input. <!DOCTYPE html> <html> <body> <script> var name = window.prompt("Enter your name: "); alert("Your name is " + name); </script> </body> </html>
๐ŸŒ
Itsourcecode
itsourcecode.com โ€บ home โ€บ how to take user input in javascript without prompt
How to take user input in JavaScript without prompt
July 6, 2023 - To receive user input without using the prompt function, you can use HTML input elements. The most simple type of input element is the text input. You can make a text input field using the tag with the type attribute set to โ€œtext.โ€ ... To ...
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ getting-user-input-in-node-js
Getting User Input in Node.js | Codecademy
Learn how to create HTML forms with various input fields to collect and validate user data. ... Node.js allows you to run JavaScript code outside of a browser window, offering powerful tools to interact with a computer filesystem, run web servers, and create terminal applications.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2952070 โ€บ javascript-user-input-solved
JavaScript User Input [SOLVED] | Sololearn: Learn to code for FREE!
I don't think you can. A solution would be: var user = prompt("your age: ") while(typeof(user) != number){ user = prompt("Please enter your age:"); } So basically, you force the user to input a number to stop showing the prompt.
๐ŸŒ
Tek-Tips
tek-tips.com โ€บ home โ€บ forums โ€บ software โ€บ programmers โ€บ web development โ€บ javascript
user input without prompt window | Tek-Tips
May 14, 2003 - <html> <body onload=&quot;getData()&quot;> <script language=&quot;javaScript&quot;> var Questions = new Array(&quot;Question 1&quot;, &quot;Question 2&quot;, &quot;Question 3&quot;, &quot;Question 4&quot; var NextQuestion=0; var Responses = new Array(); var ResponseCount=0; var IntervalID; function getData() { //populate the text box with the first question //this could be left out if you want to initialize the value //with simple HTML document.getElementById('prompt').innerHTML = Questions[NextQuestion]; IntervalID = setInterval('checkData()', 500); } function checkData() { var cBoxes = docum
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 13703759 โ€บ getting-user-input-in-javascript-without-using-prompt
Getting user input in javascript without using prompt - Stack Overflow
May 23, 2017 - I have a setup where I allow the user to type commands in a regular html textfield. I handle the onkeypress event on the textfield, check if it is an enter key that has been pressed, and parse and execute the command. ... inputControl.onkeypress = function(e) { if (e.which == 13) { var outputMessage = commandHandler.executeCommand(inputControl.value); printOutputToScreen(outputMessage); inputControl.value = ""; inputControl.focus(); } }
Top answer
1 of 2
2
Building games with prompt can be quite boring, but I got a solution for you that might spice things up with forms. Here's a simple little quiz game I built as an example for you. The game is simple just complete the word given to you. First I start with the form: index.html ```html Finish the word: Appl
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. :)
2 of 2
0
Cool. Just curious why you included it and left that extra long blank space. :) Thanks!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-take-user-input-in-javascript
How to take user input in JavaScript? - GeeksforGeeks
July 23, 2025 - The prompt method is part of the JavaScript window object. It displays a dialog box with a message to the user, an input field, and an OK and Cancel button. Users can enter text into the input field, and when they press OK, the input is returned as a string. If they press Cancel or close the dialog, null is returned. The prompt method is very helpful in situations where you require rapid and straightforward user input.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ met_win_prompt.asp
Window prompt() Method
A prompt box is used if you want the user to input a value.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2883310 โ€บ how-to-accept-user-input-when-working-with-javascript
How to accept user input when working with JavaScript?? | Sololearn: Learn to code for FREE!
Amansisa Tadese I have also struggled with this problem, so have 2 suggestions for you When trying to resolve a challenge in Playground, I replace the input with a pre-written example for the code to test var example = readline() // replace var example = "test this instead" If you wish, you could also play with this snippet written by Vasiliy https://code.sololearn.com/cdCpP5P11Ykj/?ref=app Abhay Could you elaborate further regarding prompt() as it did not work for me when I tested. ... Abhay I thought they were the same because I have only ever completed Javascript, yet am getting invites to assist with node js.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ help get user input with javascript
r/javascript on Reddit: Help get user input with JavaScript
December 12, 2011 -

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.

Top answer
1 of 2
5
var number = window.prompt("What number?");
2 of 2
3
Using document.write has some very bad side effects. The way the browser works is it parses the html markup and creates a dom tree of the page. The dom can then be accessed through javascript and styled with css. But when you use document.write, what happens is that the browser pauses the page rendering when finding a script, and goes through looking for document.writes, if it finds one it outputs it on the page. And then resumes rendering. This means you can't use document.write for anything post dom-construction as it would wipe out the page. This also means you cannot execute your scripts asynchronously or defer their executing till after the page has rendered. Techniques that can make your page load seem faster. In short the browser has to wait for every single document.write before finishing the page rendering. It is a simple looking way to add content. But will most definitely bite you in the ass. The better way to insert stuff into the page is to use, for example, innerHTML. If you're just testing and want to know what's going on in a script, console.log() might be a better option. Anyway, for any of the dom selector apis to actually work reliably you'll have to wait until the dom has been constructed or "loaded". With dom selector apis, I'm talking about stuff like document.getElementById("someid"); So in jQuery they have the $(document).ready() function. Which fixes some browser crap. But basically it sets up a event listener for "DOMContentLoaded" (which fires when the dom has been constructed) but also checks if the dom already loaded. Then there's the onload event, you'll probably see that one hanging on the body tag on a lot of sites. This event fires after every asset has finished loading. So the time from which you can start manipulating the page through scripts and the time onload fires could potentially be several seconds, or longer if the page loads huge images. Anyways, DOMContentLoaded is available in all current versions of browsers. Meaning IE9+ Then there's something like this to support all the crap browsers and quirks: https://github.com/ded/domready/blob/master/ready.js Or use one of the JS Dom libraries like jQuery to add a dom "ready" function. So for modern browsers, do something like this to execute the your javascript after dom has been constructed: function domReady(fn) { if ( document.readyState.match(/loaded|interactive|complete/) ) fn(); else document.addEventListener("DOMContentLoaded", function() { fn(); }, false); } domReady(function(){ // dom ready var output = document.getElementById("output_box"); output.innerHTML = "Replace the contents of H1"; output.innerHTML += "
Append some more stuff"; }); jQuery: $(document).ready(function(){ // dom ready });
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1731615 โ€บ please-how-do-one-get-input-from-the-user-using-pure-js-no-html
Please how do one get input from the user using Pure JS. No HTML. | Sololearn: Learn to code for FREE!
March 21, 2019 - Use the prompt build in function but note out of the is prompt is a string and if you need to get you will have to use the number build in function prompt() toNumber() ... For Example you want a user to enter his/her name in JS var input = ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to accept user input in JavaScript | JS for Beginners #javascript #js - YouTube
Learn about JavaScript user input handlling! #javascript #javascripttutorial #javascriptforbeginners #html #css #webdesign #webdevelopment #website #webdevel...
Published ย  March 18, 2025
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ beginner: how to allow javascript to access the user input in html?
r/learnprogramming on Reddit: Beginner: how to allow javascript to access the user input in html?
August 8, 2022 -

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>