Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] I.E.: 1+1=1. Worse yet, Netscape: 1+1 =___


  • From: javascript@xxxxxxxxxx (Rodney Myers)
  • Subject: [Javascript] I.E.: 1+1=1. Worse yet, Netscape: 1+1 =___
  • Date: Wed, 27 Jun 2001 23:12:37 +0100

I agree with Peter that onChange is the best event handler for text
inputs.

I would change all the handlers to
 onChange="sumboxes(this.form)"
AND include this handler in input Total_Hours__________________
Why? Because you don't want to allow any change there!

Not every input had an event handler, which explains some of your
problems.

Notice that the form object is being passed to your function and
received with (f) in

function sumboxes(f)

Peter's construction is instructive but misses two points

1. parseInt does not allow for fractions of an hour [1.5] for example.
2. There is no allowance for the removal of non-numerics.

Here is my version of sumboxes()

function sumboxes(f) {
 var totalHours = 0;

 totalHours +=hfix(f.Prep_Hours___________________);
 totalHours +=hfix(f.Office_Hours_________________);
 totalHours +=hfix(f.In_Service_Hours_____________);
 totalHours +=hfix(f.Lesson_Hours_________________);
 totalHours +=hfix(f.Travel_Hours_________________);
 totalHours +=hfix(f.Other_Hours__________________);

 f.Total_Hours__________________.value = totalHours;
 }


And here are the support functions :
The aims are to
1. Allow decimals
2. Clean up the element value
3. Return the remaining numerics as numerics

function hfix(el){

// AIM #2
// cleans up any odd input in the form
el.value=decimals(el.value);

// el.value=units(el.value); // use this if decimals NOT permitted

// AIM #3
// multiplying by 1 forces string to numeric
return(el.value.length==0 ? 0 : el.value * 1)
}

function units(str){ return(clean(str,"1234567890")) }

// AIM #2
function decimals(str){ return(clean(str,"1234567890.")) }

function clean(str,permitted){
str=""+str;
var nchar,n;var out="";
for(n=0;n<str.length;n++)
 {
 nchar=str.charAt(n);
 out+=permitted.indexOf(nchar)>-1?nchar:"";
 }
return(out);
}

One weakness of my code is that it does not check for there being more
than one decimal point.


hth

Rodney



"McCoy, Thomas" wrote:

>  Many thanks go to Peter Brunone for your help, however...I updated
> the code, but things still don't "add up"   (okay, bad
> joke). Whichever box I enter a number into first is never added to the
> total... unless I go back and retype that number once addition has
> begun.  Also, the script seems to be completely ignored by Netscape
> 4.7. I tried setting an "initial value" of "0" for each form element,
> hoping that a change from zero would trigger a response from the
> JavaScript.  It did not. The newer version of the form can be tested
> at http://www.city.newport-beach.ca.us/nbpl/literacyform04.htm.
> Thanks again for your expertise and time!

--
Shop@ssistant Add-ons and Developer Workshops
http://www.aflyingstart.net/addons/

Enquiries regarding Shop@ssistant Classic training :
Call 01256 880770

Rodney Myers
Based in Oxford, England
Technical Director, Shop@ssistant eCommerce Solutions