jquery-ui-touch-punch icon indicating copy to clipboard operation
jquery-ui-touch-punch copied to clipboard

Dynamic loaded elements are not draggable

Open ebody opened this issue 5 years ago • 3 comments

Elements loaded after events are not draggable.

<!DOCTYPE html>
<html>
    <head>
        <meta charset='UTF-8'>
        <title>Title</title>
        <meta name="description" content="">
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <meta name='viewport' content='width=device-width, initial-scale=1'>
    </head>
<body>

    <main>
        <div>Drag me!</div>
    </main>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>        
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script src='touchpunch.js'></script>
    <script src='js.js'></script>
</body>
</html>
$( document ).ready(function() {

    /**
     *  orientationchange
     */             

        $(window).on('orientationchange', function(e){
            // is not draggable
            $('main').append('<div>Orientationchange Result</div>');
        });

        $('main > div:eq(0)').click(function(){
            // is not draggable
            $('main').append('<div>Click Result</div>');
        });

    /**
     *  draggable
     */
        
        $('div').draggable({
            cursor: 'pointer'
        });

});

ebody avatar Jan 20 '20 21:01 ebody

This is not an issue with touch-punch (which turns touch gestures into mouse movements), but more to do with how jquery-ui works.

Basically, you have to re-assert the draggable after adding elements so far as I know

RWAP avatar Jan 20 '20 21:01 RWAP

Thanks... This works:

$( document ).ready(function() {

    /**
     *  orientationchange
     */             
        $(window).on('orientationchange', function(e){
            $('main').append('<div>Orientationchange Result</div>');
            makeItDraggable();
        });

        $('main > div:eq(0)').click(function(){
            $('main').append('<div>Click Result</div>');
            makeItDraggable();
        });

    /**
     *  draggable
     */
        makeItDraggable();
        function makeItDraggable(){
            $('main > div').draggable({
                cursor: 'pointer'
            });
        }
});

ebody avatar Jan 21 '20 10:01 ebody

The only problem is that you can then get multiple draggable events on existing div elements. You should ideally use something like:

$( document ).ready(function() {

    /**
     *  orientationchange
     */             
        $(window).on('orientationchange', function(e){
            $('main').append('<div c;lass="addDraggable">Orientationchange Result</div>');
            makeItDraggable();
        });

        $('main > div:eq(0)').click(function(){
            $('main').append('<div c;lass="addDraggable">Click Result</div>');
            makeItDraggable();
        });

    /**
     *  draggable
     */
        makeItDraggable();
        function makeItDraggable(){
            $('main > div. addDraggable').draggable({
                cursor: 'pointer'
            });
            $('main > div. addDraggable').removeClass('addDraggable');
        }
});

RWAP avatar Jan 21 '20 10:01 RWAP