DOM
Setup
Clone the DOM starter code.
Please refer to 10.2: Browser Applications with DOM 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.
Specifically makeBox
needs 7 steps.
Create the
p
paragraph tag. Something like:var pOne = document.createElement('p');
Fill the tag with text using
innerText
. Something like:pOne.innerText = 'haha';
Create a second
p
paragraph tag.Fill the tag with text using
innerText
.Create the
div
tag.Put both
p
tags into thediv
tag usingappendChild
. Something like:divTag.appendChild( pOne );
Put the
div
tag onto the page withdocument.body.appendChild.
Edit style.css
so you can clearly identify the box.
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.
Here is how we might set an img
tag's src
attribute in JS.
Edit style.css
for makeCard
to better identify our cards in the UI.
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.
Trigger the makeCard
function when the user clicks the "Make Card" button by calling addEventListener
on the button with makeCard
as the callback function.
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
Get another image URL from the internet
Search for an image
Right click on the image in Chrome
Select "Copy Image Address"
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.
Last updated