I think this is what you're looking for. You can use eq() to get a certain index and index to get the current index. Since you structured your .cart_product
div according to your other div
you can just get the current index of the clicked div
and use that to show/hide the corresponding div
$(document).ready(function() {
var num;
$('.up').click(function() {
num = parseInt($(this).siblings('.product').text());
$(this).prevAll('.product').text(num + 1);
$('.cart_product').eq($(this).closest('.product_box').index()).show();// <-- on up click you will only ever show a product
});
$('.down').click(function() {
num = parseInt($(this).siblings('.product').text());
if (num > 0) {
$(this).siblings('.product').text(num - 1);
}
if (parseInt($(this).siblings('.product').text()) == 0) { <-- check if current val in div is == 0
$('.cart_product').eq($(this).closest('.product_box').index()).hide(); <-- if it is the hide the .cart_product div with the same index as the current "div"
} else {
$('.cart_product').eq($(this).closest('.product_box').index()).show();
}
});
});
http://jsfiddle.net/7XnXF/12/