Stop dragging with a conditional when condition is reached: issues

Stop dragging with a conditional when condition is reached: issues

Hi there, I've been reading the post:
https://forum.jquery.com/topic/how-to-cancel-drag-while-dragging
People suggest to use trigger('mouseup') to stop the dragging and it really stops it, but it will return an error:
  1. this.helper is null
You can check it with this code:

  1. <script type="text/javascript">
    function stopit()
    {
        if($('#draggy').offset().left > 400)
        {
            $('#draggy').trigger('mouseup');
        }
    }
    $(document).ready(
        function()
        {
            $('#draggy').draggable(
            {
                axis:    'x',
                drag: function(event, ui)
                {
                    stopit();
                }
            });
        }
    );
    </script>






















If you try the following code, you will see that the 'disable' method only takes effect after the mouse is up, so it is not good to take real time actions.

  1. <script type="text/javascript">
    function stopit()
    {
        if($('#draggy').offset().left > 400)
        {
            $('#draggy').draggable('disable');
        }
    }
    $(document).ready(
        function()
        {
            $('#draggy').draggable(
            {
                axis:    'x',
                drag: function(event, ui)
                {
                    stopit();
                }
            });
        }
    );
    </script>






















I wonder how to achieve this simple task: stop the dragging when reached a certain value.
Can somebody help me with this??