Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu January 19, 2024 › #HowTo #Javascript

How to display user input in JavaScript?

In this article, you will learn two methods to display user input in JavaScript.

Method 1: Using HTML Form and JavaScript #

To display user input in JavaScript, one common method is by utilizing HTML forms and JavaScript to retrieve and showcase the entered data. Here's how you can achieve this:

  1. Create an HTML form with an input field and a button to submit the data.
  2. Use JavaScript to capture the input value when the form is submitted.
  3. Update the HTML content to display the captured user input.

Explanation of the above example:

  1. An HTML form is created with an input field and a button.
  2. The JavaScript function displayInput() is called when the button is clicked.
  3. The input value is captured using getElementById() method.
  4. The captured value is displayed in the <p> element with the ID output.

Method 2: Using prompt() Function #

Another way to display user input is by using the built-in prompt() function to get input from the user and show it directly in an alert or on the page.

1. Use the prompt() function to request user input.
2. Store the input in a variable.
3. Display the user input using alert() or by updating the HTML content.

Explanation of the above example:

  1. The HTML contains a button that triggers the displayPrompt() function.
  2. Inside the function, the prompt() function is used to gather user input.
  3. The captured input is displayed in the <p> element with the ID output.

Conclusion: #

In this article, you learned two methods to display user input in JavaScript.

The first method involves creating an HTML form and using JavaScript to capture and display the input, while the second method uses the prompt() function to directly get user input and display it.

save
listen
AI Answer
Write Your Answer
loading
back
View All