SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Help please...

  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help please...

    hi. someone made this script for me and im really grateful i like what does but it could be better if you can help me tweak this.

    this script is use to calculate averages, thing is it only average exact numbers, i want it to also accept numbers with decimals. heres the code. ill really appreciate it if somebody will tweak it for me. thank you.

    Code:
    <html>
       <head>
          <title>test</title>
          <style type="text/css">
             .default{
                width: 300px;
                padding: 3px;
                text-align:left;
                font:bold 10pt sans-serif;
                background-color:F2F2F2;
                font-size:11px;
                border:1px solid #000000;
             }
             .calcAvgInput{
                float:right;
                background-color:#FFFFFF;
                border:1px solid #7F9DB9;
                font:normal 10pt sans-serif;
             }
          </style>
       </head>
       <body>
          <div class="default" style="background-color:#D9EDDD;">
             <b>Calculate Average:</b>
          </div>
          <div class="default">
             <input type="text" name="value" id="valueID" value="0" class="calcAvgInput" /> Last Call :
          </div>
          <div class="default">
             <input type="text" name="divisor" id="divisorID" value="0" class="calcAvgInput"  style="background-color:#D9EDDD;font:bold 10pt sans-serif;" readonly /> Number's of call :
          </div>
          <div class="default">
             <input type="text" name="total" id="totalID" value="0" class="calcAvgInput" style="background-color:#D9EDDD;font:bold 10pt sans-serif;" readonly /> Total Handle Time :
          </div>
          <div class="default">
             <input type="text" name="average" id="averageID" value="0" class="calcAvgInput" style="background-color:#D9EDDD;font:bold 10pt sans-serif;" readonly /> Average Handle Time :
          </div>
          <div class="default">
             <input type="button" value="Calculate Average" class="calcAvgInput" onclick="calcAverage();"/>
          </div>
          <script type="text/javascript">
             function calcAverage(){
                if(isNaN(parseInt(objAmount.value))){
                   alert("Enter only numbers and try again.");
                } else {
                   objDivisor.value = parseInt(objDivisor.value) + 1;
                   objTotal.value = parseInt(objTotal.value) + parseInt(objAmount.value);
                   objAverage.value = (objTotal.value / objDivisor.value);
                }
             }
             
             var objTotal = document.getElementById("totalID");
             var objAmount = document.getElementById("valueID");
             var objAverage = document.getElementById("averageID");
             var objDivisor = document.getElementById("divisorID");
          </script>
       </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of parseInt(), try parseFloat() or Number()

  3. #3
    SitePoint Enthusiast kceleb9's Avatar
    Join Date
    Jun 2008
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get rid of ParseInt... it is not necessary in this script - who ever wrote it is using it as a poor mans validation - but it is not even doing that correctly.

    If you need input validation too - consider a more robust validation library. The DOM element acquisition code there is also fairly clumsy.

  4. #4
    SitePoint Member
    Join Date
    May 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tnx for the advice. ill try those..

  5. #5
    SitePoint Enthusiast kceleb9's Avatar
    Join Date
    Jun 2008
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JQuery is a Javascript library with a pretty good form validation plug in.


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
  •