Change your opening body
tag to this:
<body onload="placeName();">
Then, your JS file should look like this:
function placeName(){
var name = prompt("Enter name");
if (name != null){
var greeting = "Hello " + player + "!";
document.getElementById("demo").innerHTML = greeting;
}
}
You also need to make sure that you have a div
or something of the sort with the id demo
to actually change when the function
runs. That's extremely important. The only thing that's different above from what you did is that it waits until the body loads to prompt the user. Your JS file may have loaded before your document
, which could cause issues. Also make sure that you have linked your JS file to your HTML file or have put it in script
tags in the head. If none of that works, check your console for errors and put it in a JSFiddle so that we can see all of your code to check for errors.