views:

25

answers:

2

Hi Experts,

I am trying to create a stage. I have 5 items in a draggable div. There clones will be dragged to 20 droppable areas.

I have created 20 boxes of divs with different ids. Means multiple drop able areas.

The problem is that I don't have the idea that how can I get id the of the div where the item is dropped and how, I will show that product only in that div. Right now it is displaying all of the items in a single first div.

The main problem is that how can we create multiple droppable areas dynamically and drop an item in that specific area.

Thanks in Advance

A: 

jQuery-ui Draggable Droppable event handler

You need to get the droppable dom object from the event arguments passed to the drop method.

$( ".selector" ).droppable({
  drop: function(event, ui) { 
    // $(this) represents the droppable.
    alert($(this).attr("id"));
  }
});

the draggable object can be referenced through ui.draggable. Most of this was taken from the jQuer-ui documentation found at the jQuery-ui Website

Adding Droppable to a Dynamically Added Element

In the event that the elements, let's call them stage targets, are being added dynamically to the "stage" then you would want to call the droppable method on those elements when they are created.

function makeStageTargets(i) {
  for(i;i--;true){
    var d = $("div").attr("id","item_"+i); // this will make a div id item_i
    $("stage").append(d);
    d.droppable(
      drop:function(e, ui){
        var param = $(ui.draggable).attr('src');
        $("stage").remove(ui.draggable); // this will remove an item when dropped
        addlist(param);
      });
  }
}
makeStageTargets(60);

If I have missed the spirit of your question please advise.

Gabriel
Thanks Gabriel for your reply.But the problem is, where comes the selector part in $( ".selector" ).I have multiple multiple divs more than, 20 generated dynamically with ids like item_1, item_2 .. etc. Now at this point my item is dropped in, how I will be changing it dynamically.Once again thanks for your help.
kakaajee
I think we are in the right direction, but there are total 5 items and they can be dropped multiple times, means clone of the item will be dropped there, and one thing more to restrict the user to drop a single item in droppable area.Many thanks Gabriel. I was wondering around from many days, now I am getting some clue with your help.
kakaajee
A: 

Here is the code I am using

$("div_item").droppable({

        drop:
            function(e, ui)
                {
                    var param = $(ui.draggable).attr('src');

                    addlist(param);
                }

});

Where addlist() is a function to get ajax contents and display in div like $('#div_item').append(msg.txt);

'msg' variable to get the item detail from ajax file.

The problem is with id div_item. These are more than 20, colud be upto 60. I also want to restrict each div with a single dropped in it. Many thanks for your help

kakaajee
generally the idea is to edit a question with the new relevant information. Answer posts should be kept for answers. I have updated my answer to answer what I believe your question to be. Though for clarity your question(s) are:How do I add an element to the page dynamically?How do I make that element droppable?How do I make the items that are dropped onto it only droppable once?I have attempted to address these questions in my answer.
Gabriel