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"/> 

1
  • 1
    Is a page refresh happening? Commented Mar 14, 2016 at 13:21

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

epascarello is right. By default the button will be submit button. On clicking it will submit and refresh the page
Thanks for the help !
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"/>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.