Sequence of functions problem

swordfishsoup
swordfishsoup used Ask the Experts™
on
Hi
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
$(function(){
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*/		

$("#50").click(function(){			
qty=(50);
$("#tblqty").html(qty);
$("#tblunit").html(unitcore);
$("#tblsubtotal").html((unitcore*qty).toFixed(2));
$("#tbldel").html(5.00.toFixed(2));
$("#tbltotal").html((unitcore*qty + 5.00).toFixed(2));
});

$("#100").click(function(){
qty=(100);
unitedit = (unitcore -0.20).toFixed(2);
$("#tblqty").html(qty);
$("#tblunit").html(unitedit);
$("#tblsubtotal").html((unitedit*qty).toFixed(2));
$("#tbldel").html(10.00.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();
            $("#tblqty").html(content);

        }
    });
});
 });

Open in new window

Comment
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:

   
$("input[id^='qty_']).click(function(){
        qtySelected = $(this).attr("id").substr(4);
        if(!isNaN)
        {
             processQuantity(qtySelected);
        }
        else
        {
            // grab and/or validate value entered in "Other" textbox
            var otherQuantityEntered = $("#otherQuantityTextbox").val();
            if(!isNaN(otherQuantityEntered))
            {
                processQuantity(otherQuantityEntered);
            }
            else
            {
                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