Show the result by clicking the button

Asked

Viewed 124 times

0

I have this button, but when I click this it displays the value, not what is typed.

<input type="text" id = "numero"/>
<input type="submit" id = "adivinhar" value = "Clique aqui"/>

<script>

    var AcessarElemento = document.getElementById("adivinhar");
    AcessarElemento.onclick = botaoClicado;

    function botaoClicado() {
        alert(AcessarElemento.value);
    };

</script>

1 answer

0


You are displaying the value of the button itself clicked when you do AcessarElemento.value, you need to select the field <input type="text" id = "numero"/> by your id, see the example below:

//Seleciona o botão pelo id
var AcessarElemento = document.getElementById("adivinhar");
AcessarElemento.onclick = botaoClicado;
//seleciona o input que contém o valor que deseja exibir
Numero = document.getElementById("numero");
function botaoClicado() {
  //exibe o valor do campo
  alert(Numero.value);
};
<input type="text" id = "numero"/>
<input type="submit" id = "adivinhar" value = "Clique aqui"/>

Browser other questions tagged

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