0
I wanted to know what is the error in this code. The button is set, yes, and the console pointing this error.
<script type="text/javascript">
window.onload = function() {
var botao = document.getElementsByTagName('button');
function funcaoAuxiliar(j) {
alert('Você clicou o botão ' + j);
};
};
for (var i=0; i<botao.length; i++) {
botao[i].onclick = funcaoAuxiliar(i);
}
</script>
<button type="button">botao0</button>
<button type="button">botao1</button>
<button type="button">botao2</button>
<button type="button">botao3</button>
<button type="button">botao4</button>
<button type="button">botao5</button>
Why the
for
is out of thewindow.onload
? In fact, dobotao[i].onclick = funcaoAuxiliar(i)
will not work because you are running the function instead of assigning it to the event.– Woss