how to display answer on the same page?

+1 Parul Bhagat · January 10, 2016
In the code below the answer is being displayed on a refreshed page . What should I do to print my answer on the same page below the submit button and not on a new refreshed ?


Post a Reply


Oldest  Newest  Rating
0 Nicholas Eason · January 12, 2016
From what I understand (from googling the documentation/use of document.write()), document.write automatically called, which in turn forces a refresh. So to write to a location, you could use something like a blank paragraph,
   function calc(){

       //Whatever you had here before, but instead of document.write...
       document.getElementById("theID").innerHTML = ans;
<!-- Note that I intentionally leave the paragraph blank. Whenever the user presses the button, it just updates the    paragraph's text to be whatever the answer is. -->
<p id="theID"></p>

Side note, all those ?'s are just my trying to press space 4 times to simulate a tab. I'll report the bug.
0 Glenn Cook · January 12, 2016
Try putting the java script in the
// put function here
 in the opening body tag
 <body onload="calc()"> 
 I will try running this when I have the chance, and see if that works.... 
  • 1



A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.