I'm trying to get a handle on proper jQuery syntax and functions. I'm not sure I'm doing this in an efficient way. Here is my function that I'm storing as a variable so I can repeat it:
var addProduct = function() {
$('#' + thisProduct).addClass('added');
$('#' + thisProduct + ' .more-info').addClass('hide');
$('#' + thisProduct + ' .panel-collapse').removeClass('in');
};
And then this is how I am using it:
$('#purple-shirt .add').click(function(){
thisProduct = 'purple-shirt';
addProduct();
// plus something unique to this product
$('.something-different').show();
});
...
$('#green-shoe .add').click(function(){
thisProduct = 'green-shoe';
addProduct();
// plus something unique to this product
$('.another-thing').addClass('foo');
});
I feel like maybe this is a really weird way to do it? I have to redefine the variable each time.
I thought maybe I could do an each function, like this:
$('.add').each(function(){
$(this).parent().click(function() {
});
});
But I got thrown off because some of the "products" will have unique actions in addition to the repeating function, that aren't always directly related to parent() or closest() etc... There are lot of things happening with each different product.
The code I'm using works - I'm just not sure it's the right way or very efficient.
Thanks!