Show result by clicking on button

0

I have this button, but when I click this it displays the value, not the one that 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>
    
asked by anonymous 06.06.2016 / 21:02

1 answer

0

You are displaying the value of the button you clicked when you do AcessarElemento.value , you need to select the <input type="text" id = "numero"/> field for 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"/>
    
06.06.2016 / 21:15