<div class="box">
<div class="item red">red</div>
<div class="item yellow">yellow</div>
<div class="item blue">blue</div>
</div>
const child = document.querySelectorAll('.item');
let box = document.querySelector('.box');
child.forEach(el => {
el.addEventListener('click', () => {
if(el.matches('.red')){
box.style.background = "red";
}
if(el.matches('.yellow')){
box.style.background = 'yellow';
}
if(el.matches('.blue')){
box.style.background = 'blue';
}
})
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.