0

As a drag event stops, I must find if my div collides with any other node. So, on the "stop" event, I write a loop that will check for collisions with items of the same type.

$('.article').draggable({

    stop: function(event, ui){

        $(".article").each(function (i) {

            if(this == ui.helper){
                return;
            }

            // Test collisions
        });         
    }
});

Now, I can't find the way to exclude itself from that loop. I expected this and ui.helper to be the same, but they're considered separate elements. How should I do this?

Thanks,

Jem
  • 6,226
  • 14
  • 56
  • 74
  • How can your elements collide? Can you provide us a [fiddle example](http://jsfiddle.net/)? – alexbusu Sep 10 '12 at 14:46
  • 1
    Your example doesn't work because `this` is a raw DOM node, and ui.helper is a jQuery object. Also, you can't compare two jQuery objects: `$('body') === $('body') // false`. – Alexey Lebedev Sep 10 '12 at 14:52
  • @AlexanderV.B. using the function found here: http://stackoverflow.com/questions/5419134/how-to-detect-if-two-divs-touch-with-jquery – Jem Sep 10 '12 at 14:54

2 Answers2

6

I presume you want to exclude this from $('.article'). If so, this can easily be achieved with the not method:

$('.article').not(this).each(function (i) {
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
1

Another possible way when the results are siblings within the same parent

$(this).index() == $(ui.helper).index()
Robin Maben
  • 22,194
  • 16
  • 64
  • 99