简体   繁体   中英

javascript displaying user input after a prompt

I am wanting to display what the user entered after the user has finished inputting in information. When the user gets to the last prompt I want the page to display what they entered in. For example: if they entered in apples under the name portion I want it to list the word apples in the P1 section of my code at the bottom. I am new to javascript and in advance, I know my code is choppy but I am learning so please go easy on me and please help me figure out how to display user-input after the javascript has finished.

My code:

<!DOCtype html>
<html> 
<head>
<title> This will display some answers </title> 
<body>
<script type="text/javascript">

var user = prompt("Welcome to learning about allocations with me, mr fields. 
In this tutorial we will be learning about alloction exceptions and what not 
to do with them. Let's get started, shall we? ").toUpperCase(); 

 switch(user){
    case 'YES':
     var user_1 = prompt("What's your name?"); 
     switch(user_1) {
        case 'Buster':
            prompt("Hey, brother!");
            break;
        case 'Alex':
            prompt("I've made a huge mistake.");
            break;
        case 'Steve':
            prompt("Steve Holt!");
            break;
     default:
         prompt("I don't know you!");}
break; 
default:
    prompt("too bad!"); 
}
</script>
 </body>
 </head>
 <h1> answer </h1> 
 <p1> "answers should go here" </p1> 

</html> 

 var user_1 = prompt("What's your name?"); var answerElement = document.getElementById("answer") answerElement.innerText = user_1;
 <p id="answer"></p>

To print the value of the variable in Javascript you need to add it to the DOM (document object model) as one of the nodes in the DOM tree. Here is a simple example to play around with and learn (below).

IMPORTANT: please check how the HTML document should be properly structured as well. Good luck.

<!DOCTYPE html>
<html> 
    <head>
        <title>This will display some answers</title> 
        <script type="text/javascript">
            window.onload = function(){
                var username = prompt("What is your name?");
                var usernameElement = document.getElementById("name");
                usernameElement.innerText = username;
            }
        </script>
    </head>
    <body>
        <p id="name"></p>
    </body>
</html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM