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

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Alert Box (unstructured code)

<input type="button" value="Click me" onclick="alert('Thanks... I feel much better now!');">

Alert Box (structured code)

<head>
<script>
// Wait for DOM to load
document.addEventListener("DOMContentLoaded", function(event) {

  // Put the button into a variable
  var e = document.getElementById("go");
  
  // Wait for user to click the button  
  e.addEventListener("click", function() {
  
      // Do the alert box
      alert("Hello");
      
  }, false);
  
});
</script>
</head>
<body>
<form name="myForm">
  <input id="go" type="button" value="Click me">
</form>
</body>