You are here: irt.org | FAQ | JavaScript | Misc | Q1112 [ previous next ]
Something like this:
<html> <head> <script language="JavaScript"><!-- function makeProduct(Name, Code, Price) { this.Name = Name; this.Code = Code; this.Price = Price; } Product = new Array(); Product[0] = new makeProduct('Widget','jaw1024',2.05); Product[1] = new makeProduct('Floopy','jaw1025',45.10); function getProduct(Code) { var Found = false; for (var i = 0, n= Product.length;i<n;i++) { if (Product[i].Code == Code) { Found = true; break; } } if (Found) return i; else return -1; } function calcPrice(theForm) { var ItemNumber = getProduct(theForm.theCode.value.toLowerCase()); if (ItemNumber ==-1) { alert('Sorry no such thing'); return; } var theProduct = Product[ItemNumber]; theForm.theName.value = theProduct.Name; theForm.thePrice.value = theProduct.Price * parseInt(theForm.theQuantity.value); } //--></script> <form> <input type="text" name="theCode"> Code <br><input type="text" name="theQuantity"> Quantity <br><input type="button" value="Get and Calc" onClick="calcPrice(this.form)"> <br><input type="text" name="theName"> Name <br><input type="text" name="thePrice"> Total Price </form>