mingfish_004
mingfish_004

Reputation: 1413

how can i remove draggable event for div?

how can i remove draggable event for div?

I want to add new .point to container , then draggablge it, but firstly I want to unbind all draggable events for $('.piont'), how to do it ?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.8.2/jquery-ui.js"></script>
<style type="text/css">
    *{margin:0;padding:0}
    .container{
        margin:50px;
        width:100px;
        height:100px;
        border:1px solid #777;
        position:relative
    }
    .point{
        width:20px;
        height:20px;
        background-color:tan;
        position:absolute;
    }
</style>
<script type="text/javascript">
    $(function(){
        $('.point').draggable({
            stop: function (e){
                alert(1);
            }
        }); 
    });

    function unbind_draggable(){
        // how can unbind draggable for .point?
    }
</script>
<div class="container">
    <div class="point"></div>
    <div class="point"></div>
    <div class="point"></div>
    <div class="point"></div>
</div>
<button onclick="unbind_draggable()">unbind_draggable</button>

enter image description here

Upvotes: 1

Views: 6200

Answers (4)

Robin
Robin

Reputation: 7895

To remove draggable from all .points, use:

function unbind_draggable(){
    $(".points").draggable("destroy");
}

That will remove all draggable functionality, not just disable it.

See the jQuery UI API.

Although just disabling will work, I don't recommend it as it will not clear the RAM that the jQuery draggable is occupying.

Upvotes: 2

super
super

Reputation: 2328

For removing draggable event :

 $( ".selector" ).draggable( 'disable' ); OR
 $( ".selector" ).draggable( 'destroy' );

Further details check jQuery UI API documentation. Click here

For your question:

function unbind_draggable(){
     $('point').draggable( 'destroy' );
     //OR
     $('point').draggable( 'disable' );
}

Upvotes: 7

Amber Sharma
Amber Sharma

Reputation: 61

 $('.point').droppable('disable');

Upvotes: 0

Aditya Singh
Aditya Singh

Reputation: 9612

Try this out:- http://jsfiddle.net/adiioo7/chyhf/

JS:-

$(function () {
    $('.point').draggable({
        stop: function (e) {
            alert(1);
        }
    });
});

function unbind_draggable() {
    $('.point').draggable("destroy");
}

Upvotes: 0

Related Questions