Skip to content

Instantly share code, notes, and snippets.

@jeffersontpadua
Last active April 23, 2019 23:10
Show Gist options
  • Save jeffersontpadua/35d4751c85012a6c43e98a8c4a54d703 to your computer and use it in GitHub Desktop.
Save jeffersontpadua/35d4751c85012a6c43e98a8c4a54d703 to your computer and use it in GitHub Desktop.
const candidatoVotado = document.getElementById('candidato-votado');
const identificacaoVoto = document.getElementById('identificacao-voto');
const corrigir = document.getElementById('corrigir');
const confirmar = document.getElementById('confirmar');
const fotoCandidato = document.getElementById('foto-candidato');
const form = document.getElementById('formulario');
if(!candidatoVotado) {
throw new Error('Nenhum input com o id candidato-votado adicionado');
}
if(!identificacaoVoto) {
throw new Error('Nenhum H1 com o id identificacao-voto adicionado')
}
if(!corrigir) {
throw new Error('Nenhum button com o id corrigir adicionado')
}
if(!fotoCandidato) {
throw new Error('Nenhum button com o id foto-candidato adicionado')
}
const digitos = document.getElementsByClassName('digito-voto');
function alterarVoto(voto) {
candidatoVotado.value = voto;
identificacaoVoto.innerHTML = '#' + candidatoVotado.value;
fotoCandidato.style.backgroundImage = "url('resources/img/" + voto + '.jpg' + "')";
}
function onClickDigito(digito) {
alterarVoto(candidatoVotado.value + digito);
}
for(let index = 0; index < digitos.length; index++) {
console.log('Adicionando click events');
const item = digitos.item(index);
item.addEventListener('click', onClickDigito.bind({}, item.innerHTML))
}
corrigir.addEventListener('click', () => {
alterarVoto('');
});
confirmar.addEventListener('click', () => {
form.submit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment