JavaScript: the DOM, events, and essentials

Tuesday, 10/06 · Week 6

Post-Class Notes

TL;DR#

Today was the first full JavaScript session: we read the to-do app your agent wrote on 10/01 by changing it and watching the page, then worked through the DOM and events in the DOM and events slides. Thursday starts with your Mini Project 2 wish list. The habit worth keeping: before you delete or change a line, predict what the page will do, then check.

Before next class#
  1. Mini Project 2 opened today. On Thursday you will write your wish list in logs/wk06.md in class, trade it with a classmate and pick your idea. The suggested pace in the spec now starts from Thursday.
  2. Predict Before You Delete. In your oim3690/js/todo.js, pick three lines: an addEventListener, a line that changes the page (textContent, classList or appendChild), and one of your choice. For each one, write in logs/wk06.md what the page will do without it, delete it, reload and try the page, then note whether you were right and put the line back.
  3. Make It Yours. Ask your agent for one feature your to-do does not have yet: editing a task, a due date, or buttons that show only done or only open tasks. Before you keep it, find each new addEventListener and name the element and the event it waits for. Then commit and push.
  4. logs/wk06.md on GitHub by Sunday 10/11. Open your repo on GitHub and check that it is there.

Checkpoint 3 closes Sunday 10/11.

Learning code by changing it#

You do not need to write JavaScript from memory to understand it. Change one thing, predict what will happen, and check the page:

  • Deleting the word function broke the page, and the Console tab in DevTools named the line.
  • Commenting out event.preventDefault() made a ? appear in the address bar after adding a task. Without it, the form submits to its own page, the same way the Google search form from 10/01 sent ?q=Babson.
  • Deleting const changed nothing visible. That does not make it optional; see the correction below.
Where the to-do keeps your tasks#

The tasks you typed last week were still there after closing VS Code, but they are not in todo.html. They are in your browser's local storage. To see them, open DevTools, choose the Application tab, then Local storage and your page. They are saved as JSON, the same format as the agent settings you pasted on the first day: curly brackets, square brackets, and "key": value pairs. JSON.stringify() turns the list into text to save, and JSON.parse() turns it back into a list when the page loads.

What runs first#

A correction to something said in class: a script does not wait for its last line. The browser runs the top-level lines from top to bottom as it reaches them, so const form = document.querySelector(...), let todos = loadTodos() and every addEventListener run on the way down. Only the inside of a function waits until something calls it. render() on the last line is the last thing to run and the first thing to draw the list.

Two more corrections:

  • const matters. Your page kept working without it only because a plain <script> lets an undeclared name become a global variable. In stricter code the same line stops with an error. Thursday's JavaScript essentials covers variables.
  • The main reason the <script> tag goes at the bottom of <body> is that the HTML above it already exists when it runs. In <head>, document.querySelector('#todo-form') finds nothing and returns null.
Changing any page from the Console#

The DOM is the page as a tree that JavaScript can read and change. On any site, open the Console and try:

document.title = "Changed by me";
document.body.style.backgroundColor = "red";

Nothing is saved; a reload brings the page back. Selecting works as it does in CSS: # for an id, . for a class. On babson.edu, document.querySelector('#cheader') found nothing because cheader is a class, and '.cheader' worked.

The event pattern#

Every example in the slides follows one shape: find the element, listen for an event, do something.

const button = document.querySelector("#btn");
button.addEventListener("click", function () {
  // do something
});

Swap "click" for "dblclick", "mouseover" or "mouseout" and the same code responds to a different action. Where you listen matters: a keydown listener on a button only fires while that button has focus, so a key that should work anywhere on the page goes on window:

window.addEventListener("keydown", function (event) {
  if (event.key === " ") {
    // the space bar was pressed
  }
});
Quotes in CSS and in JavaScript#

A color is a bare word in CSS and a string in JavaScript:

.box { color: red; }               /* works */
.box { color: "red"; }             /* ignored */
box.style.color = "red";           // works
box.style.color = red;             // error: red is not defined
Why the yellow background did not show#

In the toggle example, changing .hidden to set a yellow background did nothing, because the box also has an id rule:

#box { background: #eff6ff; }
.hidden { background-color: yellow; }

An id selector outranks a class selector, so #box wins whatever order the rules are in. Setting a property that #box does not set, such as color, works.

Who is responsible when an agent breaks a rule#

After one of the lightning talks, about AI agents that reached outside a test environment, we asked who is at fault: the agent, the company that built it, or the person who gave it the task.

  • Existing law already applies. Lina Khan, then chair of the Federal Trade Commission, put it this way when the FTC acted against deceptive AI claims: "there is no AI exemption from the laws on the books" (FTC, 9/25/2024). Product liability asks whether the company that sells an autonomous product made sure it does not cause harm.
  • The tool and the model are often two companies. GitHub Copilot is Microsoft's tool, and the model inside it can come from Anthropic or OpenAI.
Asking about tasks#

When someone asks whether AI will replace a job, break the job into tasks: AI helps with some and makes others worse. A 2023 field experiment with 758 consultants at Boston Consulting Group found both. On tasks inside what the authors call the "jagged technological frontier", consultants using GPT-4 finished more work, faster and at higher quality; on a task outside it, they were less likely to reach the right answer than consultants without AI (a summary by one of the authors). Vision models already take on everyday tasks for blind people: Be My Eyes with GPT-4o.