I created a simple game of the gallows, where within several multi-word themes a secret word is chosen randomly. The user has at his disposal 27 buttons, each with a letter of the alphabet and clicking a button calls a function (with 2 parameters, the corresponding letter and a numerical value) that verifies that the letter that the user chose is somewhere of the secret word. Whether or not you are after calling the function and checking the onclick property is removed.
My problem is in the buttons I have for the user to choose a new theme or new word, which returns all onclick properties to all keyboard buttons. I can add back the onclick property with the corresponding parameters however when I re-call the check () function on the buttons they all act as if they were the V letter.
Here is the clear () function that returns the onclick to all buttons, with the correct parameters.
function limpar(){
nind = 0;
tentativas = 5;
ncertas = 0;
document.getElementById("mstr").style.display = "";
document.getElementById("dca").style.display = 'none';
for(var i = 0; i < 27; i++)
{
tc[i].style.color = '#ffff03';
tc[i].onclick = function() { verificar(ar[i], i+1); };
//Adiciona a propriedade onclick de volta a cada botão, ar[i] é a letra do array correspondente ao botão tc[i] e o i+1 o valor numérico correspondente
}
for(var i = 0; i < paltd.length; i++)
{
paltd[i].innerHTML = "-";
paltd[i].style.display = 'none';
}
document.getElementById("ptnt").innerHTML = tentativas;
}
And here is the function to check that you receive the letter and number corresponding to the button you clicked.
function verificar(b,num){
//alert(b, num);
var nerr = 0;
for (i = 0; i < palavrasecreta.length; i++)
{
if(b == palavrasecreta[i])
{
paltd[i].innerHTML = b;
ncertas += 1;
tc[num-1].style.color = '#60ff90';
}else{
nerr +=1;
}
}
if (nerr == palavrasecreta.length)
{
tentativas -= 1;
document.getElementById("ptnt").innerHTML = tentativas;
tc[num-1].style.color = '#773333';
}
//alert(ncertas);
tc[num-1].onclick = function(){};
verificarjogo(ncertas);
}
My problem is that for some reason the check () function is always checking as if I had clicked the V button, it does not matter which button I click. When inspecting element I see that all buttons have their respective parameters, however when clicking they always call the check function () with parameters V and 24.
The first time the game runs, everything works fine, it only happens after calling the clean function that resets the game, returning each button to its onclick property.
Edit: I forgot to mention that the keyboard are buttons of the same class in an html page arranged in qwerty format, and each number corresponds to its position in the class. V for example is the 24th button (23 in the class)