Detailed explanations & code examples are in the JavaScript (.js) file
Click the { } button at the bottom right to view it
Create, Manipulate, and Interact with HTML Elements
Dynamically create and add HTML elements using JavaScript
| Step | Method | Purpose |
|---|---|---|
| 1. Create | document.createElement(tag) |
Creates new element |
| 2. Configure | .textContent, .style, .id |
Set properties/attributes |
| 3. Add to DOM | .append(), .prepend(), .insertBefore() |
Place element in page |
| Method | Where Element Goes |
|---|---|
.append() |
End of parent (last child) |
.prepend() |
Beginning of parent (first child) |
.insertBefore(new, ref) |
Before reference element |
.removeChild() |
Removes element from DOM |
Make elements interactive with mouse events
| Event | When It Fires |
|---|---|
click |
Element is clicked |
mouseover |
Mouse enters element |
mouseout |
Mouse leaves element |
mousedown |
Mouse button pressed down |
mouseup |
Mouse button released |
Hover over and click the box!
Respond to keyboard input and arrow keys
| Event | When It Fires |
|---|---|
keydown |
Key is pressed down |
keyup |
Key is released |
keypress |
Key is pressed (deprecated) |
Use arrow keys to move the box!
event.key - The key that was pressed (e.g., "a", "Enter", "ArrowUp")event.preventDefault() - Prevents default browser action (e.g., scrolling).startsWith() - Checks if string starts with specified characters
Two methods to hide elements: display and visibility
| Method | Hidden Value | Visible Value | Takes Space? |
|---|---|---|---|
display |
none |
block |
β No (removes from layout) |
visibility |
hidden |
visible |
β Yes (keeps space) |
Five ways to select HTML elements in JavaScript
| Method | Selector | Returns | Can use .forEach()? |
|---|---|---|---|
getElementById() |
ID only (no #) | Single element or null | N/A (single element) |
getElementsByClassName() |
Class only (no .) | HTMLCollection (live) | β Need to convert to array |
getElementsByTagName() |
Tag name | HTMLCollection (live) | β Need to convert to array |
querySelector() β |
Any CSS selector | FIRST matching element | N/A (single element) |
querySelectorAll() β |
Any CSS selector | ALL elements (NodeList) | β Yes! |
// Method 1: By ID (fast, but ID only) const header = document.getElementById("header"); // No # needed // Method 2: By class (returns live collection) const boxes = document.getElementsByClassName("box"); // No . needed Array.from(boxes).forEach(box => { }); // Need to convert // Method 3: By tag (returns live collection) const paras = document.getElementsByTagName("p"); // Method 4: querySelector - FIRST match β const firstBox = document.querySelector(".box"); // Need . for class const navBtn = document.querySelector("#nav > button"); // Complex CSS // Method 5: querySelectorAll - ALL matches β const allBoxes = document.querySelectorAll(".box"); allBoxes.forEach(box => { }); // .forEach() works directly!
querySelector() and querySelectorAll() for modern code - they're more flexible and work with any CSS selector!
One-line explanations for commonly used methods
| Method/Property | Simple Explanation |
|---|---|
.toFixed(n) |
Rounds number to n decimal places, returns string |
location.reload() |
Reloads/refreshes the current page |
isNaN() |
Checks if value is Not a Number (true/false) |
.scrollWidth |
Total width of content including hidden overflow |
.clientWidth |
Visible width of element (excludes scrollbar) |
.innerHTML |
Gets/sets HTML content inside element (including tags) |
getComputedStyle() |
Gets actual computed CSS styles of element |
DOMContentLoaded |
Event fires when HTML is fully loaded |
Loading...