SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculate fields?!?

    I'm trying to make this work, but something is not right?!

    PHP Code:
    <?php require_once('js/createturn_calc.php'); ?>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><div align="center"><input type="textbox" name="black_1" onblur="calc()" value="" class="scorecard"></div></td>
            </tr>
            <tr>
              <td><div align="center"><input type="textbox" name="black_2" onblur="calc()" value="" class="scorecard"></div></td>
            </tr>
            <tr>
              <td><div align="center"><input type="textbox" name="black_3" onblur="calc()" value="" class="scorecard"></div></td>
            </tr>
            <tr>
              <td><div align="center"><input type="textbox" readonly name="black_total" class="scorecard" value=""></div></td>
            </tr>
    </table>
    createturn_calc.php:
    Code JavaScript:
    <script type="text/javascript">
    <!--
     
    function calc(){
    // Gathers field information
     
    // BLACK
    var b1 = validNum(document.scorecard.elements['black_1'].value);
    var b2 = validNum(document.scorecard.elements['black_2'].value);
    var b3 = validNum(document.scorecard.elements['black_3'].value);
     
    // Calculates Information
    var black = b1 + b2 + b3;
     
    document.scorecard.elements['black_total'].value = black;
     
    }
     
    // Makes sure variable is a number
    function validNum(nmbr){
    // forces variable into integer type
    nmbr = (nmbr * 1);
     
    // Checks if variable is Not a Number
    if(!isNaN(nmbr))
    // If a number, return value
    return nmbr;
    else
    // If not a number, return zero
    return 0;
    }
    -->
    </script>

    It gives an error return...

    document.scorecard.elements is null or not a object...

    Where do I go wrong?

  2. #2
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    JavaScript is not my strong point but I think that this:

    document.scorecard.elements['black_total'].value = black;

    Should really be this:

    black = document.scorecard.elements['black_total'].value;
    TAKE A WALK OUTSIDE YOUR MIND.


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
  •