Sequence of functions problem

swordfishsoup used Ask the Experts™
I have a product selection process on my site and I'm working on the final section which is the pricing based on quantities chosen.

I have added notes to the attached code so it should be clear what I'm trying to do. Basically, a unit price is initially taken from a session variable.

Then 7 buttons 6 with standard quantities 50, 100, 200 up to 500 and the last 'other' where a quantity can be typed in.

I have tried to do 'switch case (I'm new to all this so don't entirely understand that) and a chain of 'if else' statements. I assumed that by setting first in effect' if
qty >=50 <=100 then unit price = unitprice
but if >100 <200 unitprice = unitprice (minus) .10p etc

I then would be able to use that info for all the buttons and the 'other' quantity option.

I can't get this to work. The basic functions are ok (in my code) but I'm sure there must be a way to define the calcs and then apply that to whichever quantities are selected.

I hope that makes some sense, please ask if not.

Thank you
var unitcost =  $('.unit').html();
		var unitcore = (1*(unitcost)).toFixed(2); /*taken from session variable*/
		var unitedit; /*I added this so not to permantly change the main unit cost when other quantites are selected*/
		var qty;
		var del;/*delivery*/
		var total
		var subtotal
/* There are 7 buttons with preset quantities and an 'other' button where alternate quantities can be selected*/		

$("#tbltotal").html((unitcore*qty + 5.00).toFixed(2));

unitedit = (unitcore -0.20).toFixed(2);
$("#tbltotal").html((unitedit * qty + 10.00).toFixed(2));

/*I need the quantity added in to this text box to be calculated like 'if qty >50 <100' etc*/

$(function() {
    var content = $('#txtbox').val();

    $('#txtbox').keyup(function() { 
        if ($('#txtbox').val() != content) {
            content = $('#txtbox').val();


Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What I would do is first prefix all your "quantity" buttons with something like "qty_", e.g. qty_50, qty_100, qty_other, etc.

now you can both identify these buttons as a group:

    $("input[id^='qty_']) // matches any input with id starting with "qty_"

Now, connect this selector to a click method, and you've still got your value info embedded in your ID so it can be split out for easy use:

        qtySelected = $(this).attr("id").substr(4);
            // grab and/or validate value entered in "Other" textbox
            var otherQuantityEntered = $("#otherQuantityTextbox").val();
                alert("Please ensure that the quantity entered is a number.");

    function processQuantity(quantity)
         // put your common code for handling the quantity here!

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial