Skip to content

About

Session 17: JavaScript & DOM ๐ŸŒ

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒ JavaScript & DOM Manipulation

๐Ÿ“ธ Lesson Snapshot

Learn JavaScript basics and interact with web pages using the DOM! We'll compare JavaScript to C# and build an interactive Book Library webpage. (Ages 8-18)

Session Date: Saturday, March 21, 2026 at 11:00 AM EST


๐ŸŽฏ Age-Tiered Learning Goals

๐Ÿง’ Elementary (8-11) ๐Ÿง‘ Middle School (12-14) ๐ŸŽ“ High School (15-18)
๐Ÿ”ค JS Basics Understand variables and functions Use arrays and objects Compare C# and JS patterns
๐ŸŒณ DOM Select elements by ID Use querySelector and loops Create dynamic content
๐ŸŽฎ Events Handle click events Work with forms and input Build event-driven apps
๐Ÿ’พ Storage See data persist Use localStorage basics Build full CRUD with storage
๐Ÿ By End Click buttons to change page Add/remove books dynamically Complete library with persistence

๐ŸŽจ Before You Code: Hands-On Activities (15 minutes)

๐ŸŽฎ Activity 1: The Document Object Model - Human Tree (Whole Class)

๐ŸŽฌ Setup: Students physically arrange themselves to form a DOM tree

๐Ÿ“– Scenario: Building a Web Page

    THE DOM IS A FAMILY TREE! ๐ŸŒณ

    One student is <html> (the grandparent)
              โ”‚
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                   โ”‚
  <head>             <body>
(one student)     (one student)
    โ”‚                   โ”‚
    โ”‚           โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  <title>    <header>  <main>  <footer>
                        โ”‚
                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”
                <h1>        <div>
                             โ”‚
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”
                     <p>         <button>


    INSTRUCTIONS:
    1. Students hold signs with tag names
    2. They link arms with their "parent" and "children"
    3. Teacher calls out: "Find the <button>!"
    4. Trace the path: html โ†’ body โ†’ main โ†’ div โ†’ button

๐Ÿ’ฌ Discussion:

"JavaScript uses the DOM to find and change elements. When we say document.getElementById('myButton'), we're asking JavaScript to walk down this family tree and find that element!"


๐Ÿ’ƒ Activity 2: C# vs JavaScript - Translation Dance (Pairs)

๐Ÿ”„ One partner says C#, the other translates to JavaScript:

    C# TO JAVASCRIPT TRANSLATION ๐Ÿ”„

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  C# (What you know)         โ”‚  JavaScript (New!)           โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                              โ”‚
    โ”‚  string name = "Sam";       โ”‚  let name = "Sam";           โ”‚
    โ”‚        โ†“                    โ”‚       โ†“                      โ”‚
    โ”‚  [Declare type explicitly]  โ”‚  [Let JS figure out type]    โ”‚
    โ”‚                             โ”‚                              โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                              โ”‚
    โ”‚  int age = 12;              โ”‚  let age = 12;               โ”‚
    โ”‚                             โ”‚                              โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                              โ”‚
    โ”‚  void SayHello() { }        โ”‚  function sayHello() { }     โ”‚
    โ”‚                             โ”‚                              โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                              โ”‚
    โ”‚  Console.WriteLine(x);      โ”‚  console.log(x);             โ”‚
    โ”‚                             โ”‚                              โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                              โ”‚
    โ”‚  List<string> books =       โ”‚  let books = [];             โ”‚
    โ”‚    new List<string>();      โ”‚                              โ”‚
    โ”‚                             โ”‚                              โ”‚
    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚                             โ”‚                              โ”‚
    โ”‚  books.Add("Harry Potter"); โ”‚  books.push("Harry Potter"); โ”‚
    โ”‚                             โ”‚                              โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    ๐ŸŽฏ KEY INSIGHT: JavaScript is more relaxed about types!
    C# says: "Tell me EXACTLY what type this is!"
    JavaScript says: "I'll figure it out, don't worry!"

๐Ÿ’ก Key Insight:

"You already know programming! JavaScript just uses different words for the same ideas."


๐Ÿƒ Activity 3: Event Listener Telephone (Groups of 5)

๐Ÿ“ฑ Pass a message through the chain:

    EVENT FLOW - Like a Game of Telephone! ๐Ÿ“ž

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  USER   โ”‚โ”€โ”€โ”€โ–ถโ”‚ BROWSER โ”‚โ”€โ”€โ”€โ–ถโ”‚   DOM   โ”‚โ”€โ”€โ”€โ–ถโ”‚  YOUR   โ”‚โ”€โ”€โ”€โ–ถโ”‚ PAGE    โ”‚
    โ”‚ clicks  โ”‚    โ”‚ detects โ”‚    โ”‚ creates โ”‚    โ”‚  CODE   โ”‚    โ”‚ UPDATES โ”‚
    โ”‚ button  โ”‚    โ”‚  click  โ”‚    โ”‚ "event" โ”‚    โ”‚  runs   โ”‚    โ”‚   โœจ    โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    ROLE PLAY:
    1. ๐Ÿ‘† Student 1 (User): "I clicked the Add Book button!"
    2. ๐Ÿ–ฅ๏ธ Student 2 (Browser): "Click detected at button#addBook!"
    3. ๐Ÿ“‹ Student 3 (DOM): "Creating click event with target: addBook"
    4. ๐Ÿ’ป Student 4 (Code): "Running addBook function!"
    5. โœจ Student 5 (Page): "New book card appears on screen!"

    This is EXACTLY what happens with:
    button.addEventListener('click', addBook);

๐ŸŽฏ Takeaway:

"Events are how your webpage LISTENS for user actions. You're setting up a listener that waits patiently until something happens!"


๐Ÿ—บ๏ธ Visual: The DOM Tree

    HOW JAVASCRIPT SEES YOUR WEB PAGE ๐ŸŒณ

                          document
                              โ”‚
                              โ”‚
                            <html>
                              โ”‚
                โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                โ”‚                           โ”‚
             <head>                       <body>
                โ”‚                           โ”‚
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚             โ”‚          โ”‚         โ”‚         โ”‚
      <title>       <link>    <header>   <main>   <footer>
    "Book Library"  CSS file     โ”‚         โ”‚
                                 โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”
                              <h1>  <form>   <div>
                                           id="bookList"
                                              โ”‚
                                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”
                                        โ”‚     โ”‚     โ”‚
                                     <div>  <div>  <div>
                                     book1  book2  book3


    ๐Ÿ” FINDING ELEMENTS:

    document.getElementById('bookList')
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚
              โ–ผ
    Returns the <div id="bookList"> element!

    document.querySelector('.book-card')
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚
              โ–ผ
    Returns the FIRST element with class "book-card"

    document.querySelectorAll('.book-card')
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚
              โ–ผ
    Returns ALL elements with class "book-card" (as a list!)

๐Ÿ—บ๏ธ Visual: Event Flow

    HOW EVENTS WORK IN JAVASCRIPT ๐ŸŽฏ

    STEP 1: SET UP A LISTENER ๐Ÿ‘‚
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    const button = document.getElementById('addBtn');

    button.addEventListener('click', handleClick);
    โ”‚           โ”‚              โ”‚         โ”‚
    โ”‚           โ”‚              โ”‚         โ””โ”€โ”€ Function to run
    โ”‚           โ”‚              โ””โ”€โ”€ What event to listen for
    โ”‚           โ””โ”€โ”€ "Hey, listen for events!"
    โ””โ”€โ”€ The element we're watching


    STEP 2: USER INTERACTS ๐Ÿ‘†
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

         ๐Ÿ–ฑ๏ธ User clicks the button
                   โ”‚
                   โ–ผ
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚   CLICK EVENT! ๐Ÿ’ฅ    โ”‚
         โ”‚                     โ”‚
         โ”‚  target: <button>   โ”‚
         โ”‚  type: "click"      โ”‚
         โ”‚  timestamp: 12345   โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
         Your function runs! ๐Ÿš€


    STEP 3: YOUR CODE RESPONDS ๐Ÿ’ป
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    function handleClick(event) {
        console.log("Button was clicked!");
        // Add a new book, show a message, etc.
    }


    COMMON EVENTS ๐Ÿ“‹
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    'click'     โ†’ User clicks something
    'submit'    โ†’ Form is submitted
    'input'     โ†’ Text is typed in a field
    'keydown'   โ†’ Key is pressed
    'mouseover' โ†’ Mouse hovers over element
    'load'      โ†’ Page finishes loading

๐Ÿ“ Folder & File Map

Session17/
โ”œโ”€โ”€ README.md           โ† ๐Ÿ“– This lesson plan
โ”œโ”€โ”€ index.html          โ† ๐ŸŒ The webpage structure
โ”œโ”€โ”€ styles.css          โ† ๐ŸŽจ Makes it look pretty
โ”œโ”€โ”€ script.js           โ† ๐Ÿ’ป JavaScript code (the magic!)
โ””โ”€โ”€ .gitignore          โ† ๐Ÿšซ Files Git should ignore

โœ… Prerequisites

  • โœ… Completed Sessions 1-15 (C# fundamentals & API)
  • โœ… Understanding of variables, functions, and loops
  • โœ… Basic understanding of HTML from Session 16

๐Ÿ› ๏ธ Step-by-Step Build Guide

1๏ธโƒฃ Fork and Clone

  • Go to GitHub and Fork the Session17 repository
  • Clone to your computer: git clone https://github.com/YOUR-USERNAME/Session17-JavaScript.git

2๏ธโƒฃ Explore the HTML Structure

๐Ÿ“„ Open index.html and examine:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>๐Ÿ“š My Book Library</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <h1>๐Ÿ“š My Book Library</h1>
    </header>

    <main>
        <!-- ๐Ÿ“ Form to add new books -->
        <form id="bookForm">
            <input type="text" id="titleInput" placeholder="Book Title" required>
            <input type="text" id="authorInput" placeholder="Author" required>
            <button type="submit">โž• Add Book</button>
        </form>

        <!-- ๐Ÿ“š Where our books will appear -->
        <div id="bookList"></div>
    </main>

    <script src="script.js"></script>
</body>
</html>

๐Ÿ” Key parts:

    HTML STRUCTURE BREAKDOWN ๐Ÿ“‹

    <form id="bookForm">
    โ””โ”€โ”€ Has an ID so JavaScript can find it!

    <input id="titleInput">
    โ””โ”€โ”€ We'll get the typed value from here

    <div id="bookList">
    โ””โ”€โ”€ Empty now, JavaScript will fill it with books!

    <script src="script.js">
    โ””โ”€โ”€ Loads our JavaScript file (put at bottom!)

3๏ธโƒฃ Connect JavaScript to HTML

๐Ÿ“„ Open script.js and start coding:

// ๐ŸŽฏ STEP 1: Wait for the page to load
document.addEventListener('DOMContentLoaded', function() {
    console.log('๐Ÿš€ Page loaded! JavaScript is ready!');

    // All our code goes inside here
});

๐Ÿ’ก Why wait for DOMContentLoaded?

    PAGE LOADING TIMELINE โฑ๏ธ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ 1. Browser starts downloading HTML                          โ”‚
    โ”‚ 2. Browser parses HTML, building the DOM tree               โ”‚
    โ”‚ 3. Browser hits <script> tag                                โ”‚
    โ”‚ 4. JavaScript tries to run...                               โ”‚
    โ”‚                                                             โ”‚
    โ”‚    โŒ WITHOUT DOMContentLoaded:                             โ”‚
    โ”‚    JavaScript might run before elements exist!              โ”‚
    โ”‚    document.getElementById('bookList') = null ๐Ÿ˜ฑ            โ”‚
    โ”‚                                                             โ”‚
    โ”‚    โœ… WITH DOMContentLoaded:                                โ”‚
    โ”‚    JavaScript waits until DOM is fully built               โ”‚
    โ”‚    document.getElementById('bookList') = <div> ๐ŸŽ‰          โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

4๏ธโƒฃ Select DOM Elements

๐Ÿ“„ Add to script.js:

document.addEventListener('DOMContentLoaded', function() {

    // ๐ŸŽฏ STEP 2: Grab references to our HTML elements
    const bookForm = document.getElementById('bookForm');
    const titleInput = document.getElementById('titleInput');
    const authorInput = document.getElementById('authorInput');
    const bookList = document.getElementById('bookList');

    console.log('๐Ÿ“ Form found:', bookForm);
    console.log('๐Ÿ“š Book list found:', bookList);

});

๐Ÿ” Compare to C#:

    SELECTING ELEMENTS: C# vs JavaScript

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  C# (Finding a control in WinForms/WPF)                     โ”‚
    โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”‚
    โ”‚  TextBox titleBox = this.Controls.Find("titleInput");       โ”‚
    โ”‚                                                             โ”‚
    โ”‚  JavaScript (Finding an element in DOM)                     โ”‚
    โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”‚
    โ”‚  const titleInput = document.getElementById('titleInput');  โ”‚
    โ”‚                                                             โ”‚
    โ”‚  Same concept! Different syntax! โœจ                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

5๏ธโƒฃ Create Your First Array

๐Ÿ“„ Add a books array:

document.addEventListener('DOMContentLoaded', function() {
    // ... element references from Step 4 ...

    // ๐ŸŽฏ STEP 3: Create an array to store our books
    let books = [];

    // ๐Ÿ“š Let's start with some sample books
    books = [
        { id: 1, title: 'Harry Potter', author: 'J.K. Rowling' },
        { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien' },
        { id: 3, title: 'Diary of a Wimpy Kid', author: 'Jeff Kinney' }
    ];

    console.log('๐Ÿ“š Books:', books);
});

๐Ÿ” Compare to C#:

    ARRAYS/LISTS: C# vs JavaScript

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  C# (Typed List with Class)                                 โ”‚
    โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”‚
    โ”‚  List<Book> books = new List<Book>                          โ”‚
    โ”‚  {                                                          โ”‚
    โ”‚      new Book { Id = 1, Title = "Harry Potter" },           โ”‚
    โ”‚      new Book { Id = 2, Title = "The Hobbit" }              โ”‚
    โ”‚  };                                                         โ”‚
    โ”‚                                                             โ”‚
    โ”‚  JavaScript (Flexible Array with Objects)                   โ”‚
    โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”‚
    โ”‚  let books = [                                              โ”‚
    โ”‚      { id: 1, title: 'Harry Potter' },                      โ”‚
    โ”‚      { id: 2, title: 'The Hobbit' }                         โ”‚
    โ”‚  ];                                                         โ”‚
    โ”‚                                                             โ”‚
    โ”‚  ๐Ÿ”‘ KEY DIFFERENCES:                                        โ”‚
    โ”‚  โ€ข JS uses let instead of List<Type>                        โ”‚
    โ”‚  โ€ข JS objects use { } without "new"                         โ”‚
    โ”‚  โ€ข JS properties are lowercase by convention                โ”‚
    โ”‚  โ€ข JS uses single quotes ' ' (or double " ")                โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

6๏ธโƒฃ Display Books on the Page

๐Ÿ“„ Add a function to render books:

// ๐ŸŽฏ STEP 4: Function to display all books
function displayBooks() {
    // Clear the current list
    bookList.innerHTML = '';

    // Loop through each book and create HTML
    books.forEach(function(book) {
        // Create a new div for this book
        const bookCard = document.createElement('div');
        bookCard.className = 'book-card';
        bookCard.setAttribute('data-id', book.id);

        // Set the content
        bookCard.innerHTML = `
            <h3>๐Ÿ“– ${book.title}</h3>
            <p>โœ๏ธ by ${book.author}</p>
            <button class="delete-btn" onclick="deleteBook(${book.id})">
                ๐Ÿ—‘๏ธ Delete
            </button>
        `;

        // Add it to the page
        bookList.appendChild(bookCard);
    });

    console.log('๐Ÿ“š Displayed', books.length, 'books');
}

// Call it to show initial books
displayBooks();

๐Ÿ” What's happening:

    CREATING ELEMENTS DYNAMICALLY ๐Ÿ—๏ธ

    document.createElement('div')
    โ””โ”€โ”€ Creates a new <div> element (not on page yet!)

    bookCard.className = 'book-card'
    โ””โ”€โ”€ Adds the CSS class for styling

    bookCard.innerHTML = `...`
    โ””โ”€โ”€ Sets the HTML content inside the div
        (The ` ` backticks allow multi-line strings!)

    bookList.appendChild(bookCard)
    โ””โ”€โ”€ Actually adds the element to the page!

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Before appendChild:     โ”‚  After appendChild:              โ”‚
    โ”‚                          โ”‚                                  โ”‚
    โ”‚  <div id="bookList">     โ”‚  <div id="bookList">             โ”‚
    โ”‚  </div>                  โ”‚      <div class="book-card">     โ”‚
    โ”‚                          โ”‚          ๐Ÿ“– Harry Potter         โ”‚
    โ”‚  (Empty!)                โ”‚      </div>                      โ”‚
    โ”‚                          โ”‚  </div>                          โ”‚
    โ”‚                          โ”‚                                  โ”‚
    โ”‚                          โ”‚  (Book appears! โœจ)              โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

7๏ธโƒฃ Handle Form Submission

๐Ÿ“„ Add event listener for the form:

// ๐ŸŽฏ STEP 5: Listen for form submissions
bookForm.addEventListener('submit', function(event) {
    // ๐Ÿ›‘ Stop the form from refreshing the page!
    event.preventDefault();

    // Get the values from the input fields
    const title = titleInput.value.trim();
    const author = authorInput.value.trim();

    // Validate - make sure fields aren't empty
    if (title === '' || author === '') {
        alert('โš ๏ธ Please fill in both fields!');
        return;
    }

    // Create a new book object
    const newBook = {
        id: Date.now(),  // Use timestamp as unique ID
        title: title,
        author: author
    };

    // Add to our array
    books.push(newBook);

    // Update the display
    displayBooks();

    // Clear the form
    titleInput.value = '';
    authorInput.value = '';

    console.log('โœ… Added new book:', newBook);
});

๐Ÿ” Why event.preventDefault()?

    FORM BEHAVIOR ๐Ÿ“

    DEFAULT BEHAVIOR (without preventDefault):
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ 1. User clicks "Add Book"                                   โ”‚
    โ”‚ 2. Form tries to submit to a server                         โ”‚
    โ”‚ 3. Page refreshes! ๐Ÿ˜ฑ                                       โ”‚
    โ”‚ 4. All your JavaScript variables are GONE!                  โ”‚
    โ”‚ 5. Books array is reset to empty! ๐Ÿ’”                        โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    WITH preventDefault():
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚ 1. User clicks "Add Book"                                   โ”‚
    โ”‚ 2. event.preventDefault() says "STOP! Don't refresh!"       โ”‚
    โ”‚ 3. Our JavaScript code runs instead โœจ                      โ”‚
    โ”‚ 4. We handle the data ourselves                             โ”‚
    โ”‚ 5. Page stays loaded, books are safe! ๐ŸŽ‰                    โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

8๏ธโƒฃ Delete a Book

๐Ÿ“„ Add delete function (outside the DOMContentLoaded):

// ๐ŸŽฏ STEP 6: Function to delete a book
// (This needs to be global so onclick can find it)
function deleteBook(id) {
    // Confirm before deleting
    if (!confirm('Are you sure you want to delete this book?')) {
        return;
    }

    // Filter out the book with this ID
    books = books.filter(function(book) {
        return book.id !== id;
    });

    // Update the display
    displayBooks();

    // Save to localStorage
    saveBooks();

    console.log('๐Ÿ—‘๏ธ Deleted book with ID:', id);
}

๐Ÿ” Compare to C#:

    FILTERING/REMOVING: C# vs JavaScript

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  C# (LINQ)                                                  โ”‚
    โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”‚
    โ”‚  books = books.Where(b => b.Id != id).ToList();             โ”‚
    โ”‚                                                             โ”‚
    โ”‚  JavaScript (Array filter)                                  โ”‚
    โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                  โ”‚
    โ”‚  books = books.filter(book => book.id !== id);              โ”‚
    โ”‚                                                             โ”‚
    โ”‚  Same idea! Both create a NEW list without that item! ๐ŸŽฏ    โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

9๏ธโƒฃ Add Local Storage

๐Ÿ“„ Add functions to save and load:

// ๐ŸŽฏ STEP 7: Save books to localStorage
function saveBooks() {
    // Convert array to JSON string and save
    localStorage.setItem('myBooks', JSON.stringify(books));
    console.log('๐Ÿ’พ Books saved to localStorage!');
}

// ๐ŸŽฏ STEP 8: Load books from localStorage
function loadBooks() {
    const savedBooks = localStorage.getItem('myBooks');

    if (savedBooks) {
        // Parse JSON string back to array
        books = JSON.parse(savedBooks);
        console.log('๐Ÿ“‚ Loaded', books.length, 'books from storage!');
    } else {
        // No saved books, use defaults
        books = [
            { id: 1, title: 'Harry Potter', author: 'J.K. Rowling' },
            { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien' }
        ];
        console.log('๐Ÿ“š Using default books');
    }
}

๐Ÿ” What is localStorage?

    LOCAL STORAGE - Your Browser's Memory ๐Ÿ’พ

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  localStorage is like a tiny database in your browser!      โ”‚
    โ”‚                                                             โ”‚
    โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
    โ”‚  โ”‚  Browser Storage                                     โ”‚   โ”‚
    โ”‚  โ”‚                                                       โ”‚   โ”‚
    โ”‚  โ”‚  KEY              โ”‚  VALUE                            โ”‚   โ”‚
    โ”‚  โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚   โ”‚
    โ”‚  โ”‚  "myBooks"        โ”‚  "[{id:1,title:'Harry Potter'}]" โ”‚   โ”‚
    โ”‚  โ”‚  "userName"       โ”‚  "Sam"                            โ”‚   โ”‚
    โ”‚  โ”‚  "darkMode"       โ”‚  "true"                           โ”‚   โ”‚
    โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
    โ”‚                                                             โ”‚
    โ”‚  ๐Ÿ“Œ Data persists even after closing the browser!           โ”‚
    โ”‚  ๐Ÿ“Œ Each website has its own storage (can't see others)     โ”‚
    โ”‚  ๐Ÿ“Œ Limited to ~5-10MB per website                          โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    METHODS:
    localStorage.setItem('key', 'value')  โ† Save data
    localStorage.getItem('key')           โ† Read data
    localStorage.removeItem('key')        โ† Delete data
    localStorage.clear()                  โ† Delete ALL data

๐Ÿ”Ÿ Put It All Together

๐Ÿ“„ Complete script.js structure:

// ๐Ÿš€ Book Library - JavaScript Edition!

// Books array (will be loaded from storage)
let books = [];

// DOM Elements (set in DOMContentLoaded)
let bookForm, titleInput, authorInput, bookList;

// ๐Ÿ’พ Save books to localStorage
function saveBooks() {
    localStorage.setItem('myBooks', JSON.stringify(books));
}

// ๐Ÿ“‚ Load books from localStorage
function loadBooks() {
    const saved = localStorage.getItem('myBooks');
    if (saved) {
        books = JSON.parse(saved);
    } else {
        books = [
            { id: 1, title: 'Harry Potter', author: 'J.K. Rowling' },
            { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien' }
        ];
    }
}

// ๐Ÿ“š Display all books
function displayBooks() {
    bookList.innerHTML = '';
    books.forEach(function(book) {
        const card = document.createElement('div');
        card.className = 'book-card';
        card.innerHTML = `
            <h3>๐Ÿ“– ${book.title}</h3>
            <p>โœ๏ธ by ${book.author}</p>
            <button class="delete-btn" onclick="deleteBook(${book.id})">๐Ÿ—‘๏ธ Delete</button>
        `;
        bookList.appendChild(card);
    });
}

// ๐Ÿ—‘๏ธ Delete a book
function deleteBook(id) {
    if (confirm('Delete this book?')) {
        books = books.filter(book => book.id !== id);
        displayBooks();
        saveBooks();
    }
}

// ๐ŸŽฌ When page loads
document.addEventListener('DOMContentLoaded', function() {
    // Get DOM elements
    bookForm = document.getElementById('bookForm');
    titleInput = document.getElementById('titleInput');
    authorInput = document.getElementById('authorInput');
    bookList = document.getElementById('bookList');

    // Load saved books
    loadBooks();
    displayBooks();

    // Handle form submit
    bookForm.addEventListener('submit', function(event) {
        event.preventDefault();

        const title = titleInput.value.trim();
        const author = authorInput.value.trim();

        if (title && author) {
            books.push({
                id: Date.now(),
                title: title,
                author: author
            });
            displayBooks();
            saveBooks();
            bookForm.reset();
        }
    });

    console.log('๐Ÿ“š Book Library loaded!');
});

1๏ธโƒฃ1๏ธโƒฃ Commit Your Code

git add .
git commit -m "Add interactive book library with JavaScript"
git push origin main

๐Ÿ’ฌ Group Reflection (5 minutes)

๐Ÿ”„ Circle Share - Answer one:

  1. "The biggest difference between C# and JavaScript is..."
  2. "DOM manipulation lets me..."
  3. "localStorage is useful because..."
  4. "Events in JavaScript are like..."

โœ… Run & Test Checklist

  • ๐ŸŒ Open index.html in browser (double-click or Live Server)
  • ๐Ÿ“š Initial books display on page load
  • โž• Can add new books with form
  • โš ๏ธ Empty fields show alert
  • ๐Ÿ—‘๏ธ Can delete books with confirmation
  • ๐Ÿ’พ Books persist after page refresh (localStorage works!)
  • ๐Ÿ”ง No errors in browser console (F12 โ†’ Console)

๐Ÿš€ Bonus Challenges

โญ Challenge 1: Search/Filter (All Levels)

Add a search box that filters books as you type:

// Hint: Use the filter method and check if title includes search term
books.filter(book => book.title.toLowerCase().includes(searchTerm))

โญโญ Challenge 2: Edit Books (Middle+)

Add an "Edit" button that lets you modify a book's title or author.

โญโญโญ Challenge 3: Connect to Your API! (High School)

Replace localStorage with fetch() calls to your C# API from Sessions 11-15:

// Hint: Use fetch to call your API
fetch('http://localhost:5000/api/books')
    .then(response => response.json())
    .then(data => { books = data; displayBooks(); });

โญโญโญ Challenge 4: Animations (High School)

Add CSS animations when books are added or deleted:

.book-card {
    animation: fadeIn 0.3s ease-in;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}

๐Ÿ“– Vocabulary Wall

Term ๐Ÿ“ Kid-Friendly Definition ๐Ÿ’ป C# Comparison
๐ŸŒณ DOM The tree structure of your webpage that JavaScript can read and change Like a UI form's Controls collection
๐Ÿ“ let/const Ways to declare variables in JavaScript Like var or string in C#
๐Ÿ”ง function A reusable block of code Same as C# methods!
๐Ÿ“‹ Array A list that holds multiple items Like List<T> in C#
๐Ÿ“ฆ Object A container with properties Like a C# class instance
๐ŸŽฏ Event Something that happens (click, type, etc.) Like button click handlers
๐Ÿ‘‚ addEventListener Tell JS to watch for an event Like button.Click += handler
๐Ÿ’พ localStorage Browser storage that persists Like saving to a file
๐Ÿ”„ JSON Text format for data Same JSON we used in APIs!
๐Ÿ“ getElementById Find one element by its ID Like FindControl()
๐Ÿ” querySelector Find elements with CSS selectors Like LINQ queries for DOM

๐Ÿ  Take-Home Connection

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง Talk to a family member:

"Today I learned JavaScript! It's the language that makes websites interactive. When you click a button on a website and something happens without the page reloading - that's JavaScript! I built a book library where I can add and remove books, and it remembers them even after I close the browser."

๐ŸŒ Real-world examples:

  • ๐Ÿ›’ Adding items to a shopping cart
  • โค๏ธ Clicking "Like" on social media
  • ๐Ÿ” Google's search suggestions as you type
  • ๐Ÿ’ฌ Chat messages appearing in real-time

๐Ÿ“ Activity: Open your browser's Developer Tools (F12) on any website and type in the Console:

document.body.style.backgroundColor = 'lightblue';

Watch the page change! You just used JavaScript!


๐ŸŽ‰ What's Next?

    SESSION 17 COMPLETE! ๐Ÿ†

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                                                             โ”‚
    โ”‚  โœ… You learned:                                            โ”‚
    โ”‚     โ€ข JavaScript syntax (compared to C#)                    โ”‚
    โ”‚     โ€ข DOM selection and manipulation                        โ”‚
    โ”‚     โ€ข Event handling                                        โ”‚
    โ”‚     โ€ข localStorage for persistence                          โ”‚
    โ”‚                                                             โ”‚
    โ”‚  ๐Ÿ”ฎ Coming in Session 18: Fetch API & Async JavaScript      โ”‚
    โ”‚     โ€ข Connect your frontend to your C# API!                 โ”‚
    โ”‚     โ€ข Learn about Promises and async/await                  โ”‚
    โ”‚     โ€ข Build a full-stack application!                       โ”‚
    โ”‚                                                             โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

You're now a polyglot programmer - you know C# AND JavaScript! ๐ŸŽ‰

About

Session 17: JavaScript & DOM ๐ŸŒ

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages