2

I try to make a calculator with javascript with the onclick event I want the value of the button to convert an input text box. When I write the code the value of the button briefly in the text box , but it disappears instantly.

The code that i use can you find below this text, what am i doing wrong.
HTML:

    var iGetal = 0;
    function GetalRekenmachine()
    {
        iGetal = iGetal + 1;
        document.getElementById("Display").value = iGetal;
    }
 <button id="Cijfer7" value="7" onclick="GetalRekenmachine()">7</button>
    <input type="text" id="Display"/>
    


    
Zamboney
  • 2,002
  • 12
  • 22
Sandeerius
  • 377
  • 2
  • 7
  • 15

2 Answers2

9

Because the default type of a button is submit. So either cancel the click action or set the type to be button.

<button id="Cijfer7" value="7" onclick="GetalRekenmachine(); return false;">7</button>

or

<button type="button" id="Cijfer7" value="7" onclick="GetalRekenmachine()">7</button>
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

var iGetal = 0;
    function GetalRekenmachine(event)
    {
        iGetal = iGetal + 1;
        document.getElementById("Display").value = iGetal;
        event.preventDefault()
    }
<button id="Cijfer7" value="7" onclick="GetalRekenmachine()">7</button>
    <input type="text" id="Display"/>
Zamboney
  • 2,002
  • 12
  • 22