Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development
| Spring 2022
Table of Contents
Session 08 - Javascript, Part 1
Examples
Example 8.1 - Exposing Additional Form Elements
Would you like ice cream?
Yes
No
Ice Cream Options
Cup or a Cone?
Cup
Cone
Pick your toppings:
Whipped cream
Jimmies
Sprinkles
Nuts
Cherry
Submit Order
#icecream_options {display:none; background-color: #eee; margin-left: 2em; } #icecream_options.show { display: block; } label { display: block; }
document.addEventListener("DOMContentLoaded", function() { let inputYes = document.getElementById('ic_yes'); inputYes.addEventListener("click", function() { displayIceCreamOptions(); }); let inputNo = document.getElementById('ic_no'); inputNo.addEventListener("click", function() { displayIceCreamOptions(); }); }); function displayIceCreamOptions() { let divICO = document.getElementById('icecream_options'); let inputYes = document.getElementById('ic_yes'); if (inputYes.checked == true) { divICO.classList.add("show"); } else { divICO.classList.remove("show"); } }
JSFiddle - Example 8.1
Table of Contents