views:

260

answers:

2

I am using jQuery to detect drop on target, the target in question is a image. If i drag one image upon another, and then drop it, it shall replace the target source with the dropped source. This works fine until two images lies upon each other.

Some of the images lies next to each other and some times the target images overlap. if i drop, both the visible image and the image beneath is replaced with the dragged image.

$("#drag img").draggable({
  drag: function(event) { //somethings }
});

$("#target img").droppable({
  accept: '#drag img',
  drop: function() {//change src }
});

A live exampel<-Removed, try to drag an image to the right image

I hope the problem is clear and their is any good solution.

+1  A: 

Use stopImmediatePropagation to keep the event handler from being invoked on the next image. Which image is affected should be determine by the order of the event handlers. If you need it to apply only to the top image, then make sure that any image that overlaps another has it's handler applied first.

tvanfosson
Thanks, now i know what to look for, sadly i'm new to this so i don't know how to make sure it's handler applied first. Can you point me in the right direction
Cinaird
@Cinaird -- not without knowing how the items are placed on the page and how you are getting them to overlap. It could be as simple as applying a class to the images on top, then first applying the handler to the images with the class, then to those without it.
tvanfosson
@tvanfosson -- i have a live example here http://web.cinaird.se/pdf/test.htm , i have the same class on every image, is there any way to know which image is visible?
Cinaird
Try $('#Content #placeholder_1 img').add(('#Content #placeholder_0 img').droppable( ... ) with the stopImmediatePropagation call in the drop handler. You might need to check if isImmediatePropagationStopped is set on the event, first and discontinue manually if it is.
tvanfosson
Ok, i did not get the solution but i manage to solve it bit more specific for my problem. Not so good but it works, i used the underlining div to detect drop. Thanks for all the help, would i be a bit beter on jquery I'm sure it would solve my problem
Cinaird
A: 

I manage to solve it bit more specific for my problem. Not so good but it works, i used the underlining div to detect drop like this:

$("#target").droppable({
  accept: '#drag img',
  drop: function() {//change src of image in this div }
});

I don't think this will help anyone but se @tvanfosson reply for a other solution

Cinaird