[JavaScript] 1+1+1=111?

Peter Brunone peter at brunone.com
Wed Jun 27 14:35:56 CDT 2001


    Here's a revised version of your sumboxes function that does the trick.  I not only had to parseInt the values, but then I checked to see if there *was* a value (if length > 0) and changed the variable to a zero if there was nothing in the box.

    FYI, I used a one-line conditional that states the following:  set this variable (prepHours).  If (?) the length of the form field value is greater than zero, then set it equal to parseInt(f.Prep_Hours___________________.value) , and otherwise (:) set it equal to zero.


function sumboxes() { 
 var f=document.myform; 

 var prepHours = f.Prep_Hours___________________.value.length>0?parseInt(f.Prep_Hours___________________.value):0;
 var officeHours = f.Office_Hours_________________.value.length>0?parseInt(f.Office_Hours_________________.value):0;
 var inServiceHours = f.In_Service_Hours_____________.value.length>0?parseInt(f.In_Service_Hours_____________.value):0;
 var lessonHours = f.Lesson_Hours_________________.value.length>0?parseInt(f.Lesson_Hours_________________.value):0;
 var travelHours = f.Travel_Hours_________________.value.length>0?parseInt(f.Travel_Hours_________________.value):0;
 var otherHours = f.Other_Hours__________________.value>0?parseInt(f.Other_Hours__________________.value):0;

 var totalHours = prepHours + officeHours + inServiceHours + lessonHours + travelHours + otherHours;

 f.Total_Hours__________________.value = totalHours;
 }


Cheers,

Peter
  ----- Original Message ----- 
  From: McCoy, Thomas 
  To: 'peter at brunone.com' 
  Sent: Wednesday, June 27, 2001 2:02 PM
  Subject: RE: [JavaScript] 1+1+1=111?


  No luck, just popup error dialog boxes.  I am not yet proficient in JavaScript, and I think that may be a big source of my troubles.

  Here is a link to the form with the problem I described http://www.city.newport-beach.ca.us/nbpl/literacyform02.htm (enter in some numbers in the hours fields, and watch the "Total Hours" box)

  I appreciate any help you may provide!
    -----Original Message-----
    From: Peter Brunone [mailto:peter at brunone.com]
    Sent: Wednesday, June 27, 2001 11:13 AM
    To: javascript at LaTech.edu
    Subject: Re: [Javascript] 1+1+1=111?


    Thomas,

        The answer to your problems is the parseInt() function, e.g.

    var total = parseInt(formname.field1name.value) + parseInt(formname.field2name.value);

        Form data is treated as strings unless you specify otherwise.

    Cheers,

    Peter
      ----- Original Message ----- 
      From: McCoy, Thomas 
      To: 'javascript at LaTech.edu' 
      Sent: Wednesday, June 27, 2001 1:04 PM
      Subject: [Javascript] 1+1+1=111?


      Hello all!


      I am trying to create a form that uses client-side Javascript to sum numerical data.

      The form has six blanks where numbers are entered.  The seventh box will display the mathematical total of the numbers entered in the six boxes.  So far, I can only get the 7th box to either add the html tags together (not what I want) or compile the numbers in the box (if the first six contain 1,2,3,4,5,6... I get 123456 in the Total box).  Any ideas, before I pull all my hair out?


      Sincerely,
      Thomas McCoy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20010627/149b35d1/attachment.htm>


More information about the Javascript mailing list