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 ?



/images/forum/upload/2016-01-10/bf791aa16a9e6749fa401557e06a25c2.png

Post a Reply

Replies

Oldest  Newest  Rating
0 Glenn Cook · January 12, 2016
Try putting the java script in the
 <head>
<script>
// put function here
</script>
</head>
 in the opening body tag
 <body onload="calc()"> 
 I will try running this when I have the chance, and see if that works.... 
0 Nicholas Eason · January 12, 2016
From what I understand (from googling the documentation/use of document.write()), document.write automatically called document.open(), which in turn forces a refresh. So to write to a location, you could use something like a blank paragraph,
<script>
   function calc(){

       //Whatever you had here before, but instead of document.write...
       document.getElementById("theID").innerHTML = ans;
   }
</script>
<!-- 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.
  • 1

Javascript

107,260 followers
About

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

Links
Moderators