+ 2
How do I get a input from user in JS
For example if I have to make a program in which I will use input from the user for his age and then the output wil be if he has voting right or not
4 Answers
+ 4
Thanks
+ 2
Alert, prompt and confirm dialog mainly for debugging purposes.
For html/JavaScript web application, inputs from users are obtained from form and input elements.
Output message would be displayed dynamically on other html elements
Here an example:
<body>
<form onsubmit="checkAge(event)">
<input type="number" id="age" min="0" max="150" value="18" />
<input type="submit" value="Check voting right" />
</form>
<p id="message"></p>
<script>
function checkAge(e) {
e.preventDefault();
var age = e.target.age.value;
var message = document.getElementById("message");
if(age >= 18) {
message.innerText = "You have voting right";
} else {
message.innerText = "You do not have voting right";
}
}
</script>
</body>
https://code.sololearn.com/Wz7j820H0nPM/?ref=app
+ 2
Thank you
+ 1
Prompt