0

How do I ask and take user input in javascript?

I recently went over the math objects lesson in javascript and in one of the examples they took user input which was something I don't recall learning about previously.

9th Mar 2021, 4:05 PM
Andre Johnson
Andre Johnson - avatar
2 odpowiedzi
+ 3
you should avoid using prompt() (as well as alert, confirm) to get user input, and rather use HTML elements... coz they are modal dialog (blocking all page execution, annoying users, or even worse infinite loop with some of them would prevent you to properly close tab/browser or even change tab). HTML elements designed for input are... <input> wich could have different 'type' attribute (the underlying one is "string"), element reference can be retrieved by its 'id' or anything else (document.getElementById, document.querySelector...), and field value is accessible through its 'value' property/attribute... you could either watch for input events, or use a type="button" (or a <button>) to let user validate entry... input can also take a 'placeholder' attribute (visible only when 'value' is empty), and could be styled as you want: you could simulate modal dialog, without real one popping up, without blocking execution nor prevent to close tab/browser ;)
9th Mar 2021, 4:48 PM
visph
visph - avatar
+ 5
let name = prompt("Enter your name","sayed") alert(`Welcome ${name}`) in prompt function 2nd argument is default input value . where 1st one is title of prompt box.
9th Mar 2021, 4:07 PM
TOLUENE
TOLUENE - avatar