1

I guess I am to stupid to get this to work.

I have a shoppingcart which can be splitted in multiple carts, if someone buys items from different sellers. So I create for every seller his own form and table, which IDs are extended by $sellerId. Next step is to count how much elements with class 'delete16' are in the given table, for example with id="cartTable_815".

I can get this to work if I code this:

var n = $("#cartTable_815 div[class=delete16]").length;

But if I want to write myself a function to count the elements with class 'delete16' within a variable table-id I tried this:

    $("#cartInner div[class=delete16]").click(function(){
    var id = this.id;
    removeItem(id);
    });

    function removeItem(id)
    {
       var identifiers = id.split("_"); 
       var cartId = identifiers[1];
       var itemId = identifiers[2];
       var tab = "cartTable_" + cartId;
       var n = $("#cartTable_815 div[class=delete16]").length;
       alert(n);
    }

If I replace #cartTable_815 with tab it does not work.

var n = $("#cartTable_" + tab + "div[class=delete16]").length;

Hope someone can explain me how I have to do this the right way.

Thanks for your time and reading

1
  • you are missing a space var n = $("#cartTable_" + tab + " div[class=delete16]").length; Commented Aug 31, 2013 at 17:42

1 Answer 1

1

Because tab already contains a number, it probably has the value cartTable_815. So, you should be able to do:

var n = $("#" + tab + " div[class=delete16]").length;

And the selector will be:

// "#" + tab  +  " div[class=delete16]"
   "#cartTable_815 div[class=delete16]"
Sign up to request clarification or add additional context in comments.

2 Comments

Oh my god... of course. I should go to bed now... thanks a lot!
I'll do so - still have to wait 2 minutes :-) Kind regards from Spain

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.