I am new to Javascript/Jquery and is currently writing a online-shopping website as a practice. I want to write a function so that whenever I clicked the image 'buy_btn.jpg', the number of item in the shopping cart will increase by 1.
Here is my js code:
$('img[alt="buy"]').click(function(){
var item_bought = $(".shopNum").val();
item_bought++;
$(".shopNum").html(item_bought);
})
Here is my html code for the button and the shopping cart number:
<div class = "topr_bot">
<div class = "topr_bot_left">
<img src = "image/buy_btn.jpg" alt="buy"> //this is the "add to
<p>Attention: This item will ONLY provide a normal receipt</p>
</div>
<div class = "topr_bot_right">
<img src = "image/buy_btn.jpg" alt="buy2">
</div>
</div>
<div class="shopCar fr">
<span class="shopText fl">Shopping Cart</span>
<span class="shopNum fl">0</span>
</div>
The problem is, I tried to get the image by its alt tag and use alert and console.info()
to debug it, but nothing popped up/appeared. Is it because that I used the wrong way to get the image by alt, or because I should not put click()
onto a img
?
Thank you.