Get button value in js

3

Hello, I'm trying to get the value of a button and put it in a variable, but does not give a way, some help?

///HTML
<button class="teclado" id="qTeclado" onClick="clickTeclado(this.value);" value="Q">Q</button>
//JS
var letra = function clickTeclado(letra) {
document.getElementById('qTeclado').onClick = letra;
alert(letra);
}
    
asked by anonymous 14.06.2018 / 13:15

1 answer

6

You're mixing the code a bit, it's simpler:

function clickTeclado(letra) {
  alert(letra);
}
<button class="teclado" id="qTeclado" onClick="clickTeclado(this.value);" value="Q">Q</button>

That is: the clickTeclado function must be declared, and then pass this.value as argument. So, inside the function, you already have the value you want in the parameter letra of the function. In this case, you do not need the id of the button because you are calling the function directly in onclick with the value.

    
14.06.2018 / 13:18