Fundamentals (Paid)
  • 🚀Course Overview
  • Course Logistics
    • 🏫Course Methodology
      • 🧩Course Components
      • 💬Community Channels
      • 🎲Course Projects
    • 💻Required Hardware and Software
      • ☝️Required Software 1
      • ✌️Required Software 2
      • 👍Recommended Setup
    • 🗓️Schedule
    • 💡Tips and Tricks
      • 📒Coding Strategies
      • 🛠️Tooling Pro Tips
    • 🎓Post-Course
      • 🎓LinkedIn Certificates
      • 🚂Bootcamp Admission Criteria
  • 1: Introduction
    • 1.1: What is Coding?
    • 1.2: Web Browsers
    • 1.3: Command Line
    • Additional Resources 1
  • 2: Basic Data Manipulation
    • 2: Operators and Expressions
      • 2.1: Arithmetic Operators | Mathematical Expressions
      • 2.2: Assignment Operators | Variables
    • 2.3: Our First Program
    • Additional Resources 2
  • 3: Structuring and Debugging Code
    • 3.1: Functions
    • 3.2: Errors
    • Additional Resources 3
  • 4: Conditional Logic
    • 4.1: Intro to Logic
    • 4.2: Pseudo-Code, Boolean Or
    • 4.3: Boolean AND, NOT
    • 4.4: Input Validation
    • Additional Resources 4
  • 5: Managing State and Input Validation
    • 5.1: Program Lifecycle and State
    • 5.2: Program State for Game Modes
    • Additional Resources 5
  • 6: Arrays and Iteration
    • 6.1: Arrays
    • 6.2: Loops
    • 6.3: Loops with Arrays
    • Additional Resources 6
  • 7: Version Control
    • 7.1: Git
    • Additional Resources 7
  • 8: GitHub
    • 8.1: Intro to GitHub
    • 8.2: GitHub Fork and Clone
    • 8.3: GitHub Pull Request
    • 8.4: GitHub Repo Browsing
    • 8.5: Deployment
    • Additional Resources 8
  • 9: JavaScript Objects
    • 9.1: JavaScript Objects
    • 9.2: Card Deck Generation with Loops
  • 10: Advanced
    • 10.1 HTML
    • 10.2: CSS
    • 10.3: The Document Object Model
    • 10.4: DOM Manipulation
    • 10.5: Advanced Debugging with Sources Tab
  • 11: POST COURSE EXERCISES
    • DOM
    • Further Readings
  • In-Class Exercises
    • Day 2: Basic File and Data Manipulation
    • Day 3: Functions
    • Day 4: If Statements, Boolean Or, Boolean And
    • Day 5: Program State
    • Day 6: Scissors Paper Stone Redux
    • Day 7: Loops
    • Day 8: Arrays and Loops
    • Day 9: Beat That Redux
    • Day 10: Moar Cards / Chat Bot
    • Day 11: Blackjack Redux, DOM
  • Projects
    • Project 1: Scissors Paper Stone
      • Project 1: Scissors Paper Stone (Part 1)
      • Project 1: Scissors Paper Stone (Part 2)
    • Project 2: Beat That!
    • Project 3: Blackjack
  • Past Projects
    • Drawing With Emojis
    • Guess the Word
Powered by GitBook
On this page
  • Setup
  • Base
  • Creating Elements
  • Responding to HTML Events
  • More Comfortable
  • Multi-Card Button
  • More Images
  • Random Image
  • Random Number of Images
  1. 11: POST COURSE EXERCISES

DOM

Previous10.5: Advanced Debugging with Sources TabNextFurther Readings

Last updated 2 years ago

Setup

  1. Clone the .

  2. Please refer to if you need a refresher.

Base

Creating Elements

makeBox

Create an empty function called makeBox. makeBox will create a new HTML element on screen each time it's called.

If you were to write out the HTML element it would look like the following.

<div>
    <p>Make Box was Called</p>
    <p>Hello :)</p>
</div>

Specifically makeBox needs 7 steps.

  1. Create the p paragraph tag. Something like: var pOne = document.createElement('p');

  2. Fill the tag with text using innerText. Something like: pOne.innerText = 'haha';

  3. Create a second p paragraph tag.

  4. Fill the tag with text using innerText.

  5. Create the div tag.

  6. Put both p tags into the div tag using appendChild. Something like: divTag.appendChild( pOne );

  7. Put the div tag onto the page with document.body.appendChild.

Edit style.css so you can clearly identify the box.

div {
  background-color: white;
  border: 2px solid red;
  padding: 10px;
  margin: 10px;
}

Call the makeBox function inside of script.js.

makeCard

Create a function makeCard that creates card elements with the following HTML structure. See the next code snippet for how to set an img tag's src attribute in JS.

<section>
  <p>
    Hello Ducks
  </p>
  <p>
    <img src="https://gblobscdn.gitbook.com/assets%2F-MBhJa4xpezxI4J9lolG%2F-MGrF6rE0CBWVzznQayq%2F-MGrGfwNRi1D6aKWmg4G%2Fducks2.jpeg?alt=media&token=9dff244b-10e8-4fab-ab68-715e09998ff3"/>
  </p>
</section>

Here is how we might set an img tag's src attribute in JS.

imgTag.src =
  'https://gblobscdn.gitbook.com/assets%2F-MBhJa4xpezxI4J9lolG%2F-MGrF6rE0CBWVzznQayq%2F-MGrGfwNRi1D6aKWmg4G%2Fducks2.jpeg?alt=media&token=9dff244b-10e8-4fab-ab68-715e09998ff3';

Edit style.css for makeCard to better identify our cards in the UI.

section {
  background-color: grey;
  border: 2px solid blue;
  padding: 10px;
  margin: 10px;
}

Call makeCard in script.js.

Responding to HTML Events

Update the makeBox function to also render a button with the words "Make Card". The corresponding HTML might look like the following, but we will construct it using JS.

<div>
  <p>Make Box was Called</p>
  <p>Hello :)</p>
  <button>Make Card</button>
</div>

Trigger the makeCard function when the user clicks the "Make Card" button by calling addEventListener on the button with makeCard as the callback function.

var button = document.createElement('button');
button.innerText = 'Make Card';
button.addEventListener('click', makeCard);

More Comfortable

Multi-Card Button

Change the makeCard function so that it creates multiple cards when the button is clicked. We can create and append elements in a loop inside makeCard.

More Images

  1. Get another image URL from the internet

    1. Search for an image

    2. Right click on the image in Chrome

    3. Select "Copy Image Address"

  2. Put these images into an array and loop over each image in the array to create the set of cards

Random Image

Change makeCard so that when the user clicks, she gets a single card with a random image.

Random Number of Images

Add an input to the box created by makeBox. The user can fill out a number X in the input. When they click the button, X images appear inside the new card.

DOM starter code
10.2: Browser Applications with DOM