Jump to content

BTEC IT Unit 20 - Website Design/JavaScript/Prompt Box

From Wikibooks, open books for an open world

Prompt Box (structured code)

<head>
<script>
document.addEventListener("DOMContentLoaded", function(event) {

  var e = document.getElementById("go");
  
  e.addEventListener( "click", function() {

    var name=prompt("What is your name?","No name");
    if ( name!=null && name!="" ) {
      output = "Hello " + name;
      }
    else {
      output = "Hey, I asked you your name!";
      }
    
    document.getElementById( "msg" ).innerText = output;

  }, false);
  
});
</script>
<head>
<body>
<form name="myForm">
  <input id="go" type="button" value="Click this button now">
</form>
<p id="msg"></p>
</body>