I'm using some jQuery to let users manipulate images. The user should be able to double-click an image to select it, then click on some buttons which appear below for resizing or flipping the image.
The image gets passed to the image_editor function like this:
$('section img').dblclick(function () {
item_editor(this);
});
Then the item_editor function looks like this:
var item_editor = function (activeItem) {
var $activeItem = $(activeItem);
// Show border around current activeItem
$('.activeItem').removeClass('activeItem');
$activeItem.addClass('activeItem');
// Flip the selected image when the button's clicked
$('div#flip').click(function () {
$activeItem.toggleClass('flipped');
});
}
I put this up into a jsFiddle here: http://jsfiddle.net/sarahg/6sE5F/30/
The problem I'm having here is that the Flip button works the first time you use it, but if you select a different image and try to flip that one, everything which had already been flipped flips again. The activeItem variable is not what I'd imagine it should be.
I did some searching around and I think this has to do with JavaScript closures, but I haven't been able to understand them enough to make my code work.