How to display return results on my HTML page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pgrDaniel
    New Member
    • Aug 2013
    • 3

    How to display return results on my HTML page?

    Here is the HTML doc:

    Code:
    <!DOCTYPE html>
     <html>
     <head>
     <title>Testing a webpage</title>
     <link rel="stylesheet" type="text/css" href="myStyle.css">
     <script src="converter.js"></script>
     </head>
    
     <body>
    
       <form>
    //input some data
    
        <input type="button" onclick=converter(70,"fahrenheit") value="TRY">
       </form>
       
    	<p>"Result is "</p>
    	<script type="text/javascript" document.write(converter[0])></script>
       
     </body>
    
     </html>
    Here is External .js file:

    Code:
    function converter(n, from)
    {
    var res=[];
    
    if(from == "fahrenheit")
    {
    	document.write(n);
    	document.write("<br />");
    	document.write(from);
    
    	res[0] = ((5/9.0)*(n-32));
        res[1] = ((5/9.0)*(n-32)+273.15);
    	document.write("<br />");
    
    //confirm data passed
    
    	document.write("here is the result in celcius: func converter calculated: ",res[0]);
    	document.write("<br />");
    	document.write("here is the result in kelvin: func converter calculated: ",res[1]);
    	return res;   //??
    }
    }
    //The data gets passed and the calculations get done here no problem, the problem happens is I cant seem to display the results in the HTML file....its like the values dont get returned back?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the problem happens is I cant seem to display the results in the HTML file
    that’s because document.write( ) has already erased your page, including the JavaScript to display the result.

    Comment

    • pgrDaniel
      New Member
      • Aug 2013
      • 3

      #3
      Yes I see how that can be a problem...thank you. How am I going to output the answers in HTML page?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you can create HTML elements with built-in DOM methods like createElement() , innerHTML, etc.

        Comment

        Working...