Cheatsheet

Use this as a reference while working. You don't need to memorize these!

Table of Contents:

DOM (Document Object Model)

The DOM is a JavaScript representation of your HTML. It lets you select, modify, create, and delete elements on the page.

Linking JavaScript to HTML

Add a <script> tag at the end of your <body>:

For ES6 modules (import/export), add type="module":

Note: Modules require a server (use the VS Code Live Server extension).

Selecting Elements

Use document.querySelector() with CSS selector syntax:

Use querySelectorAll() to get ALL matching elements (returns a NodeList):

Modifying Elements

Creating Elements

The pattern: Create → Modify → Append

Removing Elements

Event Listeners

Listen for user interactions and run code when they happen:

Event Type
When It Fires

click

Element is clicked

mouseover

Mouse enters an element

mouseout

Mouse leaves an element

keydown

A key is pressed

keyup

A key is released

submit

A form is submitted

input

An input's value changes

The event object is passed to your handler and contains useful info:

  • event.target — the element that triggered the event

  • event.preventDefault() — stops default behavior (like form submission)

Handling Form Submissions

Alternative: Access inputs directly via form.elements:

Common DOM Patterns

Render a list from an array:

Toggle visibility:


Async

Coming soon...

Last updated