Estoy tratando de implementar un evento de clic simple pero no puedo hacerlo funcionar. Si elimino la devolución de llamada, funciona, pero al cargar no al hacer clic.
¿Alguien sabe qué está mal en mi código?
guión.js
const green = document.getElementById('green'); const clickColor = () => { alert('This is green color'); } green.onclick = () => clickColor();Puede agregar una clase al elemento en el que está haciendo clic y hacer que la función lo recoja.
const button = document.querySelector('button'); button.addEventListener('click', clickColor, false); function clickColor() { const c = this.className; console.log(`This is ${c}`); } button { color: white; } .green { background-color: green; } <button class="green">Green</button>