1

I am new to jQuery and so don't mind this question if it sounds stupid but here is something that I am trying to do :

I have 3 functions like:

AddToCart Function which adds item to the shopping cart:
//offer_id is the offer which we are trying to add to cart. 
    addToCart: function(offer_id)
    {
    },


RemoveFromCart which removes data from the cart
//target is link clicked and event is the click event. 
    removeFromCart: function(target, event)
    {
    },

Get the current state of the cart   
//return string which represents current state of cart. 
    getCartItems: function()
    {
    }

Now I am trying to do 3 things:

  1. if there is no content in cart and addToCart is called than some action, so basically here we need to check the current state of cart and that is obtained by calling getCartItems and if it is Null and than if addToCart is called than we perform some action
  2. if there is content in the cart and addToCart is called than some action,so basically here we need to check the current state of cart and that is obtained by calling getCartItems and check if it is Null or not and than if addToCart is called than we perform some action if we had some content in the cart.
  3. if there is content in the cart and removeFromCart is called some action, so basically here we need to check the current state of cart and that is obtained by calling getCartItems and if it is not Null and if removeFromCart is called than we perform some action

Pseudocode of what I am trying to do:

    if there is no content in cart 
        and addToCart is called than 
        $(document).track(
            );

    if there is content in the cart 
        and addToCart is called than
        $(document).track(
            );

    if there is content in the cart 
        and removeFromCart is called 
        $(document).track(
            );

My basic concern is that am complete newbie to jQuery and JavaScript and so am not sure how can I implement if...else logic and how can I call a function using jQuery/JavaScript.

5
  • So are you simply asking how to "implement if...else logic and how can I call a funtion using jQuery/JavaScript", or to write your code for you? Commented Apr 11, 2010 at 22:42
  • 1
    if(...){}else if(...){}else{}? If logic is JS's not Jquery's. Commented Apr 11, 2010 at 22:47
  • It's in jquery, and so not sure how to implement it. Commented Apr 11, 2010 at 22:48
  • Why would it matter if there was already content in the cart? If there is no content, add item to cart, now there is one item. If there are 4 items in the cart, add item to cart, now there are 5 items. Do the same thing regardless of previous state. I think your issue is with your actual logic. Instead of re-calculating the cart each time, just have a variable: myCart which is a basic JS object, and have it created at the start (empty), and add and remove without concern of it the cart exists or has content. Commented Apr 11, 2010 at 22:51
  • @Anthony: I understand but for report generation from Omniture side, we have this requirement and I agree it does not make sense but somehow Omniture generates better reports based on this input. Commented Apr 11, 2010 at 23:17

1 Answer 1

1

It's similar to any other programming language.

if() {
    ..
}
else if() {
    ..
}
else if() {
    ..
}

However, since these actions have to be performed when addToCart and removeFromCart are called, a simple solution is to put those conditions inside the functions itself:

addToCart: function(offer_id) {
    // add to cart is called and cart IS empty
    if(this.getCartItems().length == 0) {

    }
    // add to cart is called and cart is NOT empty
    else {

    }
}

removeFromCart: function(target, event) {
    // remove was called and cart has items
    if(this.getCartItems().length > 0) {

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.