Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Dynamic loaded elements are not draggable #324

Open
ebody opened this issue Jan 20, 2020 · 3 comments
Open

Dynamic loaded elements are not draggable #324

ebody opened this issue Jan 20, 2020 · 3 comments

Comments

@ebody
Copy link

ebody commented Jan 20, 2020

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'
        });

});
@RWAP
Copy link

RWAP commented Jan 20, 2020

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

@ebody
Copy link
Author

ebody commented Jan 21, 2020

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'
            });
        }
});

@RWAP
Copy link

RWAP commented Jan 21, 2020

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');
        }
});

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants