user3063286
user3063286

Reputation: 69

jQuery HELP - I need to make a variable that is usable multiple times

http://codepen.io/anon/pen/zvMVYv

Hey guys, basically what I'm trying to do is create a

var price = $(this).parent('figure').find('.price').text();

This variable will be used to be placed in the total cost at the bottom of the website and will have to add up every images an deduct it if an image is not selected. (I'm not sure if this is the best way to be doing things but its what I have planned).

In better terms:

when the user clicks on a highlighted image, the image is deselected (highlight should be removed) and its price should be deducted from the total.

Any help is greatly appreciated and the code pen will make more sense.

Upvotes: 1

Views: 27

Answers (1)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

Try this

ُEdited with: Update total value When you remove the highlight

var price = 0;
$('img').click(function(){
    $(this).toggleClass('highlight');
    if($(this).hasClass('highlight')){
      price += parseInt($(this).parent().find('.price > span').text());
    }else{
       price -=  parseInt($(this).parent().find('.price > span').text());
    }
    $('.cart > span').text(price);
});

working Demo

Upvotes: 2

Related Questions