Ethan
Ethan

Reputation: 167

How would you use .not(this) in the case of the following code?

How would you say :not(this) in the case of this code:

$(".draghandle").droppable({
    accept: ".draghandle:not(this)"
    drop: function ( event, ui ) {
        $( this )
        .addClass( "ui-state-highlight" )
        .find( "p" )
        .html( "Dropped!" );
    }
});

You'll see the selector right next to the accept option is probably invalid, though I haven't checked to see, I was wondering if you might have a solution as to validating it? I have yet to find one, but don't be discouraged if you have a solution that changes another part of the code as well, I just need a fix. Here's a similar question I'm trying to expand on: jQuery: exclude $(this) from selector And here's a link to the main code for this that I'm using as reference: http://jqueryui.com/demos/droppable/#accepted-elements

Upvotes: 0

Views: 2219

Answers (1)

Joseph Marikle
Joseph Marikle

Reputation: 78560

Explanation

You can pass a jQuery list as the accept argument instead of just a selector. In the following demo, I make all .ui-widget-content elements droppable except the #draggable one. (reversing the original jQuery UI site demo). the parameter I passed to accept was $(".ui-widget-content").not("#draggable").


Demo

http://jsfiddle.net/5TZ7b/
http://jsfiddle.net/5TZ7b/show


Code

$(function() {
    $( "#draggable, #draggable-nonvalid" ).draggable();
    $( "#droppable" ).droppable({
        accept: $(".ui-widget-content").not("#draggable"),
        activeClass: "ui-state-hover",
        hoverClass: "ui-state-active",
        drop: function( event, ui ) {
            $( this )
                .addClass( "ui-state-highlight" )
                .find( "p" )
                .html( "Dropped!" );
        }
    });
});

Conclusion

You can use the following as the parameter passed to accept.

$(".draghandle").not(this)

Upvotes: 3

Related Questions