Skip to content

Instantly share code, notes, and snippets.

@jmlavoier
Created June 27, 2017 21:03
Show Gist options
  • Save jmlavoier/1d836dcd73234f2c4a9c219f38763235 to your computer and use it in GitHub Desktop.
Save jmlavoier/1d836dcd73234f2c4a9c219f38763235 to your computer and use it in GitHub Desktop.
const p = document.querySelector('.p1');
const colors = ['red', 'blue', 'yellow', 'green', 'black'];
let index = 0;
const changeColor = function (event) {
event.target.style.color = colors[index];
index = index < 4 ? index + 1 : 0;
}
// Diretamente do método do elemento
p.onclick = changeColor;
// ou aplicando o evento
p.addEventListener('click', changeColor);
// ou para remover o evento
p.removeEventListener('click', changeColor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment