Grab button value in js

Asked

Viewed 3,056 times

3

Hi, I’m trying to take the value of a button and put it into a variable, but it doesn’t seem to help at all?

///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);
}

1 answer

7


You’re mixing the code a little 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 function clickTeclado has to be declared, and then you pass the this.value as argument. So, within the function, you already have the value you want in the parameter letra function. In this section you do not need the id button because you are calling the function directly on onclick with the value.

  • 1

    worked, thank you very much!!!

  • 1

    @Paulohenriquerodriguesgrund if the answer solved your problem and there was no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.