Search code examples
javascripthtmlfunctionsubmitexternal

How to return values from external javascript function


I have a function in an external java script file and I dont know how to call and return the value that my function returns in text properly. When I click place order, I want the values to be calculated by my function and then the final value to be displayed underneath the place order box. I can get my function to alert if I enter nothing but I can't get it to return my final value- what am I doing wrong?

function sum2()
{
      var one = document.getElementById("book_1").value;
      var two = document.getElementById("book_2").value;
      var three = document.getElementById("book_3").value;

      if ((one == "")||(two == "")||(three == ""))
      {
        alert ('Error', 'values missing');
      }
      else
      {
         var sum1 = one * 19.99;
         var sum2 = two * 86.00;
         var sum3 = three * 55.00;
         var sum = sum1 + sum2 + sum3;

         document.getElementById('output').value = sum;
         document.write(sum);
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Work</title>
    <script type="text/javascript" src="ex4.js"></script>
  </head>
  <body>
     <div id="container">
     <h2>Order Books Online</h2>
     <form action="" method="post" id=”frm”>
       <fieldset>
       <table border="0">
         <tr>
           <th>Book</th>
           <th>Quantity</th>
           <th>Price</th>
         </tr>
         <tr>
           <td>Basics of C++</td>
           <td><input type="text" size="3" id="book_1" /></td>
           <td>$19.99</td>
         </tr>
         <tr>
           <td>Program Development in Perl</td>
           <td><input type="text" size="3" id="book_2" /></td>
           <td>$86.00</td>
         </tr>
         <tr>
           <td>Advanced JavaScript</td>
           <td><input type="text" size="3" id="book_3" /></td>
           <td>$55.00</td>
         </tr>
       </table>
       <br /><br />
       <input type="submit" onclick="sum2(); return false;" value="Place Order" id="sub" />
       </fieldset>
      </form>
     </div>
  </body>
</html>


Solution

  • Try this, its working, output is displaying on last.

    function sum2()
    {
          var one = document.getElementById("book_1").value;
          var two = document.getElementById("book_2").value;
          var three = document.getElementById("book_3").value;
    
          if ((one == "")||(two == "")||(three == ""))
          {
            alert ('Error', 'values missing');
          }
          else
          {
             var sum1 = one * 19.99;
             var sum2 = two * 86.00;
             var sum3 = three * 55.00;
             var sum = sum1 + sum2 + sum3;
    
             document.getElementById('output').innerHTML = sum;
            // document.write(sum);
            }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Work</title>
        <script type="text/javascript" src="ex4.js"></script>
      </head>
      <body>
         <div id="container">
         <h2>Order Books Online</h2>
         <form action="" method="post" id=”frm”>
           <fieldset>
           <table border="0">
             <tr>
               <th>Book</th>
               <th>Quantity</th>
               <th>Price</th>
             </tr>
             <tr>
               <td>Basics of C++</td>
               <td><input type="text" size="3" id="book_1" /></td>
               <td>$19.99</td>
             </tr>
             <tr>
               <td>Program Development in Perl</td>
               <td><input type="text" size="3" id="book_2" /></td>
               <td>$86.00</td>
             </tr>
             <tr>
               <td>Advanced JavaScript</td>
               <td><input type="text" size="3" id="book_3" /></td>
               <td>$55.00</td>
             </tr>
           </table>
           <br /><br />
           <input type="submit" onclick="sum2(); return false;" value="Place Order" id="sub" />
           </fieldset>
          </form>
         </div>
        <div id="output"></div>
      </body>
    </html>