Name | Progress |
---|---|
JavaScript for Beginners Introduction | Not Read |
Setting up environment | Not Read |
Name | Progress |
---|---|
Section 1 | Not Read |
Arithmetic Operations | Not Read |
Type Conversion | Not Read |
Name | Progress |
---|---|
Section 1 | Not Attempted |
Section 2 | Not Attempted |
Your final task for the first module is to learn to use the window.prompt() method to solicit input from users through a dialog box and handle the input within your JavaScript code.
The window.prompt() displays a dialog box that prompts the visitor for input. It is a part of the Window interface and is used for obtaining user data. The prompt() function lets you display a message to the user and a text box where they can enter a response. The function pauses script execution until the user either submits a response or cancels the prompt.
Step-by-step guide:
From the index.html file in section 1, modify the body part by adjusting the script command by the following code:
<script>
window.prompt('What is your name? ');
</script>
The prompt displays a default text 'What is your name? ', under which the user can enter their actual name.
To display in the console, you need to modify the script by assigning the result of the prompt to a variable named username. Then, use console.log()to print a greeting to the console if a name is entered as the following code:
<script>
username = window.prompt('What is your name? ');
console.log(username);
</script>