8

I'm trying to trigger the drag event on a div immediately after wiring it up with jQuery-UI's draggable widget. I have some code that goes off when it gets dragged, and I'd like to also run this code once right after page load. I sometimes do this on other jQuery handlers using an idiom like this:

$('.foo').on('bar', function() {
   console.debug('bar!');
}).trigger('bar');

This doesn't seem to work with draggable. Here's my code:

var draggable = $('[draggable]').draggable({
   drag: function(event, ui) {
      //do some stuff
      console.debug(event);
   }
}).trigger('drag');

The drag handler gets called when I drag the item, but not on page load.

Info that might be relevant:

  1. When I drag the item, the event that shows up in the console looks like this:

    jQuery.Event {
       type: "drag",
       target: div#myElementId.ui-draggable,
       currentTarget: document,
       delegateTarget: document,
       data: null,
       //lots of other properties
    }
    
  2. I've tried various adjustments, and nothing has worked:

    • Using the jQuery.Event constructor and manually setting the above properties
    • Specifying the exact element I want with document.getElementById
    • Triggering mousedown or mousedown.draggable instead of drag (this was based on some workaround code I found)

  3. Yes, I realize I could just define the handler separately. Part of the reason I want to do this is to get access to the ui parameter, which gives me an easy way to get the element's position and offset. I could find them in other ways, but it's annoying. I'm also curious why this doesn't work.

  4. Searches on Google and SO have turned up no solutions, just workarounds that aren't relevant to me, along with other complaints about the same problem. Jörn Zaefferer from the jQUI team recommended the jQuery.simulate plugin as a workaround, so it may just not be possible. It seems odd that they wouldn't make it consistent with other jQuery events.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104

2 Answers2

4

Here is jquery ui plugin

https://github.com/jquery/jquery-ui/blob/9e8e339648901899827a58e5bf919f7dda03b88e/tests/jquery.simulate.js

include that on the page and then simply use

$("#myElement").simulate('drag');
dav
  • 8,931
  • 15
  • 76
  • 140
0

try it like this

var draggable = $('[draggable]').draggable({
    drag: function(event, ui) {
        dostuff(ui);
   }
});


function dostuff(ui){
    //do stuff with ui
}
astroanu
  • 3,901
  • 2
  • 36
  • 50
  • 1
    Thanks, but I don't think you understood the question. I'm trying to *trigger* the drag event, not attach a handler to it. In other words, I'm trying to simulate the user starting to drag the element. – Justin Morgan - On strike Jan 22 '15 at 22:13
  • I see this old thread answers a similar question but i didnt try it http://stackoverflow.com/questions/730409/jquery-ui-call-start-drag-manually – astroanu Jan 23 '15 at 03:29
  • just `drag: dostuff`.. anyways, this is the correct way +1 –  Aug 07 '15 at 14:37
  • yes it will work too :).. make sure the function is defined as dostuff(e, ui) – astroanu Aug 10 '15 at 03:51