I have a grid of images, each with a data-image attribute and value.
When a user selects an image, its data-image attributes's value is added to an array.
I want the code to work so that when the user clicks an item, if the item already is in the array (that is, if the user has clicked on it before), then it gets removed.
Like a select/de-select option.
HTML
<div class="fifth" data-image="1">
<div class="full clearfix relative">
<img src="image.png" />
<div class="overlay"></div>
</div>
<div class="options">
<h4 class="txt-white left">#0001</h4>
<h4 class="right txt-gold select-image" data-image="1">Select</h4>
</div>
</div>
jQuery
var images = [];
var price = $('.order').data('price');
// select
$('.select-image').on('click', function() {
$(this).parent().parent().toggleClass('selected');
$(this).toggleClass('selected');
if( $(this).hasClass('selected') ) {
$(this).html('✕');
} else {
$(this).html('Select');
}
images = $.grep(images, function(value) {
return value != $(this).data('image');
});
console.log(images);
images.push($(this).data('image'));
var items = images.length;
$('.total-price').html(items*price);
});
But, I can't seem to figure out how $.grep works. Any ideas?
[UPDATE]
Here's a JS Fiddle of what I have so far, you can see the issue here.