0

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> 
1
  • 1
    You should probably fix your html for starters. Place your body tag outside of the head tag and your p1 and h1 tags inside of your body tag. (also note that p1 is not a standard tag like h1 is, maybe you wanted to just use p? Also I'd recommend that your script tag(s) be the last thing inside of your body tag as well. Finally you should read up on what the DOM is and how to manipulate it here: developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/… Commented Dec 31, 2017 at 15:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.