Sequence of functions problem

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

swordfishsoupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.