[thelist] JS Calculator - Serious Help Needed

alex beston alex at deltatraffic.co.uk
Wed Apr 14 11:32:45 CDT 2004


 

>  The project is a simple
>calulator which helps clients & visitors calculate the cost of customer
>acquisition.  The first thing I need to to is this:  As a user selects a
>function from the drop-down list, the text 'Input 1' and 'Input 2' changes.
>
>User chooses 'Conversion Rate' and text changes to 'Number of Sales per
>Month' and 'Number of Visitors per Month'.
>
>  
>
 ok what you do in your .js file is to set the variable that comes in 
from the form.

first you gather info and validate;

var fieldn = document.forms[0].elements[n];

(remember to include / skip  hidden fields!)

eg something like:
 
var dropdown = document.forms[0].elements[0];
var field1 = document.forms[0].elements[1];
var field2 = document.forms[0].elements[2];

if (dropdown.value==""){
        alert("choose a value");
         dropdown.focus();
        return false ;
        }

also

if ( isNaN(field1.value) ) {
      alert( "You have non numerical characters - please enter a number  
" );
      field1.focus();
      return false ;


you go through for each form element. and if comes through you return a 
true .


now you know you have usable values  you can start to calculate

if (dropdown.value ==  "conversion rate")
    // set the operator / function to use
if (dropdown.value ==  "cost per vistor")
    // set the operator / function to use

then your function works on field1.value and field2.value and you send 
the output to a text box called  output / result with

document.forms[0].output.value.

 
so to rap up,  you do this in your <head>:

<SCRIPT language="JavaScript" src="yourcode.js" type="text/javascript"> 
</SCRIPT>

then you call on your html a function called calculate:

<input type="button" value="Calculate" onclick="calculate();">

on the first line of the calculate function you first

 if( validate()){

// do calculation

then output to the form element called ouput:

  <input name="output" size="8"   >

ofcourse nothing can be put into the output box until the validator is 
happy.

hope that helps

rgds
Alex



 




More information about the thelist mailing list