x
 
1
<!DOCTYPE html>
2
<title>My Example</title>
3
4
<script>
5
// Wait for DOM to load
6
document.addEventListener("DOMContentLoaded", function(event) {
7
8
  // Put the button into a variable
9
  var e = document.getElementById("myForm");
10
  
11
  // Wait for user to click the button
12
  e.addEventListener( "change", function() {
13
  
14
    // Put the selected value into a variable
15
    var myColor = this.color.value;
16
    
17
    // The "Switch" statement.
18
    switch ( myColor ) {
19
    
20
    case "Blue":
21
      alert("Just like the sky!");
22
      break;
23
24
    case "Red":
25
      alert("Quite daring!");
26
      break;
27
28
    case "Green":
29
      alert("Like... grass?");
30
      break;
31
      
32
    }
33
  }, false);
34
});
35
</script>
36
37
<!-- Replace '{action page}' with your own action page to support non-JavaScript users -->
38
<form id="myForm" name="myForm" action="{action page}">
39
  <label>
40
    <input type="radio" name="color" value="Blue"> Blue
41
  </label>
42
  <label>
43
    <input type="radio" name="color" value="Red"> Red
44
  </label>
45
  <label>
46
    <input type="radio" name="color" value="Green"> Green
47
  </label>
48
</form>