Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1112 How do I type in a product code (i.e jaw1024) and return the price for that product in another text box, then enter a quantity in a text box and return a total price based on quantity?

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>

©2018 Martin Webb