Re: JavaScript Form

by "Andrew Armstrong" <andrew(at)wisca.co.uk>

 Date:  Mon, 19 Nov 2001 00:55:35 -0000
 To:  "Blue Tapp" <blue(at)bluesarthouse.com>,
"HWG Techniques" <hwg-techniques(at)hwg.org>
 References:  BlueTapp
  todo: View Thread, Original
Well, I am a beginner, and not fluent enough in the coding to write an
example, but here is an *approach* that has worked in a vaguely comparable
situation:

First write a loop that runs through the form elements. Here is one that
grabs radio button names in a form:

// This function puts the names of the radio buttons in an array.
function findRadioNames(aForm){
 var n=0; //n is the radio names array index
 var LastButtonName=" ";
 for (var i=0; i<aForm.elements.length; i++){
  if(aForm.elements[i].type=="radio"){
   if (aForm.elements[i].name != LastButtonName){
   LastButtonName=aForm.elements[i].name;
   RadioNames[n]=LastButtonName;
   n++;
   }
  }
 }
return n; //n is the number of buttons to be tested.
}

Clearly there is an extra IF here because several radio buttons could have
the same name - not true for text input elements

Then I would suggest a loop that goes through all the button names in the
array, and adds the relevant value to the total one at a time as in

total+=button_value;

(Here is what I did with the radio buttons - a quick and dirty experiment
only working in Internet Explorer so far.)

//Add up selected radio buttons
var questions=0;
questions=findRadioNames(quiz);
 var total = 0;
 if (br1=="IE"){
  for (var iLoop=1; iLoop < questions; iLoop++){
// CheckQuestion returns an integer dependent on which button was checked
  total += CheckQuestion("document.quiz."+RadioNames[iLoop]);
  }
 }

//Display the answer
 document.quiz.result.value=total;

}

Andrew

----- Original Message -----
From: "Blue Tapp" <blue(at)bluesarthouse.com>
To: "HWG Techniques" <hwg-techniques(at)hwg.org>
Sent: Sunday, November 18, 2001 12:06 AM
Subject: JavaScript Form


> Hello:
> I am setting up an order form for a client and I am using JavaScript to
> figure the totals (you can see it here
> http://www.businessheadquarters.com/fresheraire/ordertest1.html).  The
user
> types in how many of each item they want and then presses the subtotal
> button.  The JavaScript multiplies the number of items by the price per
ea.
> for each item and then totals.  The formula I am using works as long as I
do
> not have more than 12 separate items in the list.  It is working now in my
> tests but I have only included the first 12 items on the list.  Is this
some
> sort of JS rule?  I am teaching myself JavaScript as I go and I am sure
> there are simpler ways to do this (I am open to all suggestions!).
> TIA!
> Blue
>
>
> Blue Tapp
> Blue's ArtHouse Graphics & Web Design
> 705 North Florissant Road
> Ferguson, MO 63135
> (314) 839-0634
> fax: (314) 839-1557
> www.bluesarthouse.com
> blue(at)bluesarthouse.com
>
> "Time to get a web site!"
>
>

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA