What I want to do is make some sort of shopping cart. The idea is that there a are several products in the page with a "buy" button below them. When you press buy, the item is added to the list in the sidebar. If there is such a product already then only the amount (number) increases.
buy prod1 -> 1 x prod1
buy prod1 -> 2 x prod1
etc.
I can't get my array done. How can I add a product to it?
Here's my code:
HTML
<div class="product">
<div class="product_img">
<img src="images/s1.png" alt="s1" width="180" height="110" />
</div>
<div class="product_descr">
<div class="descr_title">
TITLE1
</div>
<div class="descr_text">
Aliquam sed est diam lorem, iaculis malesuada enim quis nequ
</div>
<div class="descr_buy_price">
<div class="price">
2.40 Ls
</div>
<div class="buy">
<a href="#">BUY</a>
</div>
</div>
</div>
<div class="hidden">
<div class="list_item" title="i1">
<div class="item_name"> <span class="quantity"></span> x Produkts1</div>
<div class="item_price">8.99 Ls</div>
<div class="item_x">
<a href="#">
<img src="images/x.png" alt="x" />
</a>
</div>
</div>
</div>
</div>
JS
<script type="text/javascript">
$(document).ready(function() {
$('.buy a').click(function() {
var array_of_items = {};
array_of_items[$(this).parent().parent().parent().parent().find('.list_item').attr('title')] == 1;
if(array_of_items[$(this).parent().parent().parent().parent().find('.list_item').attr('title')] > 0){
array_of_items[$(this).parent().parent().parent().parent().find('.list_item').attr('title')] = +1;
$('.quantity').text(array_of_items[$(this).parent().parent().parent().parent().find('.list_item').attr('title')]);
}
else {
$(this).parent().parent().parent().parent().find('.list_item').clone().appendTo('#all_items');
$('.quantity').text(array_of_items[$(this).parent().parent().parent().parent().find('.list_item').attr('title')]);
}
});
});
</script>
.parent()s, you should try.closest().