SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using document.write to write a variable

    Hi

    I'm pretty new to JavaScript and am having a problem. I thought what I am doing should work but it isn't.

    Basically I have a form that people can put a quantity into. e.g. A, B, C.

    I have a JavaScript function called comput that assigns values e.g. A = 5, B = 7, C = 9. Here is the start of the script:

    <script language="JavaScript"><!--
    function compute(form){
    var A = form.A.value * 5;
    var B = form.B.value * 7;
    var C = form.C.value * 9;

    I then declare a variable to add them up:

    var ans = A+B+C;

    return;
    }

    </script>

    This part of the script works fine. Now I want to write the ans variable. So I use:

    <script>
    <!--
    document.write ("Your total is "+ans+"")
    // --></script>

    But it isn't working. Any ideas?

    Thanks for your help.

    M Barnes

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi mrmbarnes,

    Here are a few tips that might be helpful.

    In the following demo, you can use either of the OnSubmit functions. If you use the dom0 function then you don't need the ID attributes in the html. If you use the dom1 function then you don't need the NAME attributes in the html. The dom0 function is much more cross-browser.

    A simple 'debug msg' technique is also used in this demo. I use this often.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <script type='text/javascript'>
    function dom1_eqOnSubmit()
    {
      var x1 = parseInt(document.getElementById('eqX1').value);
      var y1 = parseInt(document.getElementById('eqY1').value);
      var x2 = parseInt(document.getElementById('eqX2').value);
      var y2 = parseInt(document.getElementById('eqY2').value);
      var m = (y2 - y1) / (x2 - x1);
      var b = y1 - (m * x1);
      dbgMsg('m = ' + m + ', b = ' + b);///debug
      document.getElementById('eqM').value = m;
      document.getElementById('eqB').value = b;
      return false;
    }
    function dom0_eqOnSubmit()
    {
      var f = document.forms['eqForm'];
      var x1 = parseInt(f.eqX1.value);
      var y1 = parseInt(f.eqY1.value);
      var x2 = parseInt(f.eqX2.value);
      var y2 = parseInt(f.eqY2.value);
      var m = (y2 - y1) / (x2 - x1);
      var b = y1 - (m * x1);
      dbgMsg('m = ' + m + ', b = ' + b);///debug
      f.eqM.value = m;
      f.eqB.value = b;
      return false;
    }
    function dbgMsg(msg)
    {
      var dbg = document.getElementById('debugMessages');
      dbg.innerHTML += '<p>' + msg + '</p>';
    }
    </script>
    </head>
    <body>
    
    <h1>Slope &amp; Y-Intercept Calculator</h1>
    <form name='eqForm' onsubmit='return dom0_eqOnSubmit()'>
    <p>Enter two points:</p>
    <p>X1 <input id='eqX1' name='eqX1' type='text' value='0' /></p>
    <p>Y1 <input id='eqY1' name='eqY1' type='text' value='0' /></p>
    <p>X2 <input id='eqX2' name='eqX2' type='text' value='20' /></p>
    <p>Y2 <input id='eqY2' name='eqY2' type='text' value='20' /></p>
    <p><input type='submit' value='Calculate' /></p>
    <p>Slope <input id='eqM' type='text' value='' /></p>
    <p>Y Intercept <input id='eqB' type='text' value='' /></p>
    </form>
    
    <h1>Debug Messages</h1>
    <div id='debugMessages'></div>
    
    </body>
    </html>

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition to that, you might want to consider this: calling document.write/writeln will clear the current document (web page) in order to make way for another. It's largely unselective, althought the actual results vary from browser to browser. Generally speaking, never call it from an event handler; always use it while a page is still loading, to 'feed' the document stream, or to write content to other window-type objects. For selective document modification, use the W3C DOM interface or innerHTML (or output to a form field).
    ::: certified wild guess :::

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good advice, adios.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •