Color Palette Picker
Using event listeners in a loop
We are going to start building a color palette picker.
Lesson Objectives
Generate 150 divs (squares) each with their own random color.
Create a click handler on each square that will add a square with the same color as the clicked square to the right column.
Generate 150 divs (squares) each with their own random color.
Create DOM elements:
<button id="generate">GENERATE</button>
<div id="container">
<div id="color-palette"></div>
<div id="my-palette"></div>
</div>div {
border: 1px solid white;
}
#container {
width: 608px;
}
#color-palette, #my-palette {
display: inline-block;
width: 300px; height: 500px;
overflow: hidden;
}
.square {
display: inline-block;
width: 28px; height: 28px;
cursor: pointer;
}Grab them with javascript:
Write a function makePalette that will generate 150 squares with the class 'square' and append them to the color-palette div
Make it so each square will have a random color
Should look like this:

Add an event listener to the generate button that will run the makePalette function
Make it so the makePalette function will empty the previous palette of squares
Give each square an event listener whose addColor handler will (eventually) add the chosen square to the right column:
Make the
addColorhandler.First, it should console.log the background color of the square that was clicked:
The
addColorfunction should:Make a new square
Give it a class of 'square'
Give it the clicked square's background color
Append to the 'my palette' right div
Last updated
Was this helpful?