1

If you build a simple dragger:

$(document).ready(function(){
  $('#tomove').draggable({
    axis: 'x',
    drag: function(event, ui) {
      mouseUp();
    }
  });
});

And you try to stop it programmatically:

function mouseUp() {
  if($('#tomove').offset().left > 400) {
    $('#tomove').trigger('mouseup');
  }
}

You will get this message in error console:

this.helper is null

Is there any way to fix this? Thanks for your help.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
Moustard
  • 347
  • 1
  • 3
  • 11

1 Answers1

0

It looks like you're just trying to constrain movement on the draggable element, is this correct? Have you seen this page: http://jqueryui.com/demos/draggable/#constrain-movement

EDIT

How about this instead then: (sample page, does what you're asking - be mindful of the jquery inclusion location)

Also notice I changed the name of the method to be something a little more apropos. This will not stop the user from being able to drag back to the left. I didn't think you wanted to actually stop them if they hit 400 (or whatever other wall).

If you want to do that, you merely $('#element').draggable('destroy')

<html>
    <head>
        <title>Draggable jQuery-UI Width Block</title>
        <script src="jquery-1.4.2.min.js" ></script>
        <script src="jquery-ui-1.8.1.custom.min.js" ></script>
    </head>
    <body>
        <div id="tomove" style="width:100px;height:20px;background:silver;">
            <span>some text</span>
        </div>
        <script>
        $(document).ready( function() {
            $('#tomove').draggable({
                axis:   'x',
                drag: function(event, ui) {
                    dragBlock( ui );
                }
            });
        });
        function dragBlock( ui ) {
            if( ui.position.left > 400 ) {
                ui.position.left = '400px';
            }
            if( ui.position.left < 0 ) {
                ui.position.left = '0px';
            }
        }
        </script>
    </body>
</html>
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • Thanks drachenstern, but this is not what I'm trying to achieve. I just want to stop the dragged object when certain value has been reached, but nothing to do with constraining. Thanks anyway for your advice. – Moustard May 26 '10 at 19:56