tags:

views:

55

answers:

1

Hello,

I have a dragable div, I want to be able to drag a div and drop into another div. It should only move inside the, dropped div.

Currently I have this code

$(document).ready(function(){
    $("#move").draggable();
    $("#move1").draggable();

    $('#contain').droppable({
        drop: function(event, ui) {
             // update the draggable to be only moveable inside the droppable
             ui.draggable.draggable("option", "containment", this);
        }
    });
});

where #contain is the div that #move and #move1 has to be placed in and moved about.

Thanks Jean

+1  A: 

Have a look at the containment option:

Constrains dragging to within the bounds of the specified element or region. Possible string values: 'parent', 'document', 'window', [x1, y1, x2, y2].

Example:

$( ".selector" ).draggable( { containment: 'parent' } );

Update: Read about the drop event. It is triggered whenever you drop a draggable onto a droppable.

Say you have your droppable #droppable, then you can do it this way:

$('#droppable').droppable({
    drop: function(event, ui) {
         // update the draggable to be only moveable inside the droppable
         ui.draggable.draggable("option", "containment", this);
    }
});

ui.draggable contains the element you just dropped.

Felix Kling
I got the divs to drag, but I cannot contain it in the <div> I drag it to.
Jean
@Jean: See my updated answer.
Felix Kling
@felix, I have updated my original Q, pls check for code
Jean
@Jean: Do you have any problems with this code? I think it should be `$('#contain').droppable({..` and it has to be `ui.draggable` instead of `ui.contain`. This is described in the documentation I linked to.
Felix Kling
I checked the doc, and re did the code, check for revised code in the QI can drop #move and #move1 into #contain, but I cannot move them inside
Jean
@Jean: Yes this is how it should be like. I created a small example http://jsbin.com/iweku you can see the source code at http://jsbin.com/iweku/edit . Tell me if this is what you want, if so, you are there ;) You should state your problem in your question too.
Felix Kling
@felix you r da man, yup thats what i want. So what exactly did I do wrong?
Jean
@Jean: I don't know, the code you posted is the same I use (you can see it here http://jsbin.com/iweku/edit). Make sure your syntax is correct. Use Firebug for Firefox the see whether you get any Javascript errors.
Felix Kling
@Felix, this is getting funny and strange. I copy/paste the entire code as/is and it does not work AT ALL, no drag, no anything....
Jean
@Jean: As I said, get a tool to debug your JS. The code itself works fine, there is not much I can do from here. If you have a link to your page, I can have a look. (btw. you really accept more answers, that is probably a reason why no one else is helping).
Felix Kling
I really accept more answers, meaning
Jean
@Jean: Oh, I missed the *should* : You really *should* accept more answers ;)
Felix Kling
@feilx How should I accept more answers, I quite didnt get what you are trying to convey.There is no error in the chrome->inspect->resources.
Jean
@Jean: People are more willing to help you, if you increase your "accepted answer" ratio. This way, the can see that you are "honoring" their effort to provide a good answer (the get reputation points for accepted answers). Your ratio is at 34% which is very low. And I bet there are more answers than only those you accepted that helped you...
Felix Kling
@feilx If the solution does work, I accept the answer, and + for that answer too.
Jean