Programming Workshop

Bite-sized exercises you can do in 2-6 hours, designed to help you master HTML, CSS, or JavaScript.

1
HTML & CSS
2
Advanced CSS
3
Beginner JS
4
Advanced JS

Mad Libs

The goal of this exercise is to make a Mad Libs generator.

The second half of these slides may be useful.

Start with this webpage, which has several input elements and a button:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8" />
    <title></title>
  </head>
  <body>
    <h1>Mad Libs</h1>
    <ul>
      <li>Noun: <input type="text" id="noun">
      <li>Adjective: <input type="text" id="adjective">
      <li>Someone's Name: <input type="text" id="name">
    </ul>
    <button id="lib-button">Lib it!</button>
    <div id="story"></div>
  </body>
  </html>

  • Add a script tag to the bottom of the page for your code.
  • Add an event listener to the button so that it calls a makeMadLib function when clicked.
  • In the makeMadLib function, retrieve the current values of the form input elements, make a story from them, and output that in the story div (like “Pamela really likes pink cucumbers.”)

Show Solution