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 "If" statement. Only display the alert if "Blue" was selected.
18
    if (myColor == "Blue") {
19
    
20
      // Output a message
21
      document.getElementById("msg").innerHTML = "Just like the sky!";
22
        
23
    }
24
  
25
  }, false);
26
});
27
</script>
28
29
<!-- Replace '{action page}' with your own action page to support non-JavaScript users -->
30
<form id="myForm" name="myForm" action="{action page}">
31
  <label>
32
    <input type="radio" name="color" value="Blue"> Blue
33
  </label>
34
  <label>
35
    <input type="radio" name="color" value="Red"> Red
36
  </label>
37
  <label>
38
    <input type="radio" name="color" value="Green"> Green
39
  </label>
40
</form>
41
42
<p id="msg"></p>