I'm doing a color picker. Every time I click on a color swatch, it updates the color and color name in another div
. I have the following code:
var indicadorColor = document.getElementById('indicador-de-color');
var indicadorMensaje = document.getElementById('indicador-de-color-mensaje');
var color = document.getElementsByClassName('color-paleta');
function seleccionarColor(){
for(i=0; i<color.length; i++){
color[i].addEventListener('click', mostrarColor)
}
function mostrarColor(){
indicadorColor.setAttribute('backgroundColor',color[i].style.backgroundColor);
indicadorMensaje.innerText = color[i].style.backgroundColor;
}
}
seleccionarColor();
But it returns undefined for color[i]
, I don't know why :(
You just have to use this instead of
color[i]
: