0
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
    </head>
        <script>
             var sum = 0
           function add(num,num2){
               var f = num * 10 + num2
               
               sum = f
               document.write('<br>','<br>',f)
           }
           
           
           
        </script>
    </body>

    
    <input type='BUTTON' value = '1' onclick="add(sum,1)">
    
    
        <input type="BUTTON" value="2" onclick="add(sum,2)">


    <input type="BUTTON" value='3' onclick="add(sum, 3)">

    <input type="BUTTON" value='4' onclick="add(sum, 4)">
    
    <body>
    
</html>

I was making a calculator in JavaScript. I made it appended to the number displayed when the number button is pressed.

But'document.write' When outputting as a function, all the existing buttons disappeared. Is it possible to prevent the button from disappearing upon printing on the web page?

Oh2010
  • 15
  • 4
  • document.write overwrites the whole document – kevinSpaceyIsKeyserSöze Sep 15 '20 at 06:57
  • `document.write` will `replace` everything each time you click on a button. Means previous button will be removed or perhaps disappear. – Always Helping Sep 15 '20 at 06:57
  • 1
    Does this answer your question? [Why is document.write considered a "bad practice"?](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) or [JavaScript Document.Write Replaces All Body Content When Using AJAX](https://stackoverflow.com/questions/2360076/javascript-document-write-replaces-all-body-content-when-using-ajax) – Ivar Sep 15 '20 at 06:58

0 Answers0