after this lesson, students will be able to:
- Generate DOM elements in a for loop
- Set a single click listener on each element generated inside the for loop
- Write a single click handler outside the loop
- Use
currentTarget
to target an element within the handler
Let's say we want to have ONE THOUSAND square divs on our page. We could write out or copy/paste 1000 divs, or we could use a for loop and do it once. Keepin' it DRY.
We are going to make 1000 square divs for our sketchpad. Each div will have the same event handler.
The goal is to generate 1000 divs in a for loop and append them to the body
- Write a loop that counts up to 1000
- Inside the loop, create a div
- Give the div a class of
square
(just for display purposes) - Append the div to the
body
- Check in the Elements tab in your console to see if you have 1000 divs with class 'square'
document.addEventListener('DOMContentLoaded', ()=>{
for (let i = 0; i < 1000; i++) {
let div = document.createElement('div')
div.className = 'square'
document.querySelector('body').appendChild(div)
}
})
Now, we can add an eventListener to the div within the loop. For our sketchpad, we don't want a 'click'. Instead, we can use a 'mouseover'.
- set an event listener one time within the loop. The first argument for the listener should be
mouseover
instead ofclick
. For the second argument, use a named handler function calledchangeColor
- Write the
changeColor
function above and outside the loop. - The handler function will add a class
pink
that will make the target element pink. To locate the target element, useevent.target
.
const changeColor = (e) => {
e.target.classList.add('pink')
}
document.addEventListener('DOMContentLoaded', ()=>{
for (let i = 0; i < 1000; i++) {
let div = document.createElement('div')
div.classList.add('square')
div.addEventListener('mouseover', changeColor)
document.querySelector('body').appendChild(div)
}
})
FIGURE IT OUT
- Using an input field, make it so that the user can decide how many divs there are!
- There will be an input box and a button. When the button is clicked, it will grab the user's input from the input box.
- The value from the input box can be used in the control panel of your for-loop
STEPS:
Add an input box and a button to the html:
<input id="inputBox" type="text" placeholder="grid size"/>
<button id="inputButton">SUBMIT</button>
- Above your for loop (NOT inside the loop) Grab both the input box and button in your js
- Set an event listener on the button.
- When it is clicked, inside the handler capture the input with
inputBox.value
.console.log(inputBox.value
Where should the code that generates your divs reside? Inside the button's click handler? What is the sequence of events?
How can you change your for loop to generate the number of divs that you grabbed from the input box?
- Make it so that when the user mouses over a square, a random color will appear, not just pink. There are many different ways to solve this problem. The important thing is that you try to tackle it rather than solve it.
- Make it so that the square divs are contained inside a container div of fixed height and width. Depending on how many divs the user decides to make, the divs should alter their size to fit the container!
Adapted from SEI-MAE