- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get number from user input and display in console with JavaScript
You can use # to get the value when user clicks the button using document.querySelector(“”); Following is the JavaScript code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <div class="container container-fluid"> <p><strong> Example:</strong> choose a number between 1 to 100</p> <form> <div class="form-group"> <label for="number">Give a number:</label> <input id="inputNumber" value="" type="text" name="number" /> </form> <p id="output"></p> </div> <button class="btn btn-dark" id="choose" type="submit">Click Me</button> </body> <script> function example() { let btn = document.querySelector("#choose"); let randomNumber = Math.ceil(Math.random() * 100); btn.onclick = function() { let givenNumber = document.querySelector("#inputNumber").value; let valueInt = parseInt(givenNumber, 100); console.log(randomNumber, givenNumber); }; } example(); </script> </html>
To run the above program, save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.
Output
Add a value −
On clicking the button Click Me, the same is visible in Console −
- Related Articles
- Make JavaScript take HTML input from user, parse and display?
- Add multiple number input fields with JOptionPane and display the sum in Console with Java
- Java Program to get text from JTextPane and display in Console
- Display a message on console while focusing on input type in JavaScript?
- How to create input Pop-Ups (Dialog) and get input from user in Java?
- How to get Input from the User in Golang?
- Python Get a list as input from user
- Java Program to Get Input from the User
- Swift Program to Get Input from the User
- Haskell program to get input from the user
- Taking input from console in Python
- Getting HTML form values and display on console in JavaScript?
- Way to read input from console in C#
- Ways to read input from console in Java
- Python program to read input from console

Advertisements