Kavya Shree
Kavya Shree

Reputation: 1032

Drag and drop in fill in blanks using Jquery

Below is my sample screenshot.

enter image description here

enter image description here

enter image description here

I had tried with drag and drop. In first 2 screenshots there will be drag and drop. Its working fine. When there will be empty box in initial. But If I set some default value in drag box it will collapse alignment. Why is it so? Below is my sample code:

$(document).ready(function() {

  function makedraggable() {
     $(".qitem2").draggable({
        "revert": "invalid"
     });
  }

  makedraggable();

     $(".box1").droppable({
        "accept": ".qitem2",
        "drop": function(event, ui) {
            if ($(this).find(".qitem2").length) {
                var $presentChild = $(this).find(".qitem2"),
                    currChildId = $presentChild.attr("id"),
                    $currChildContainer = $("#" + currChildId + "-container");                  
               $currChildContainer.append($presentChild);
               $presentChild.removeAttr("style").removeClass("drag-center-in-droppable");
               makedraggable();
            }

            $(ui.draggable).clone().appendTo(this).removeAttr("style").addClass("drag-center-in-droppable");
            $(ui.draggable).remove();

        }
     })
     function makedraggable1() {
     $(".qitem3").draggable({
        "revert": "invalid"
     });
  }

  makedraggable1();
});

PHP code

<div id="drag_paragraph1">  
<p style="display:inline;font-size:14px;line-height:200%">
In order to polymeric</p><div class="fancy nonEditable" draggable="true"><div class="box4 ui-droppable"></div></div>macromolecules into smaller portions,<div class="fancy nonEditable" draggable="true"><div class="box4 ui-droppable"></div></div>digestive enzymes play a vital role in the of<div class="fancy nonEditable" draggable="true"><div class="box4 ui-droppable"></div></div>absorption of nutrients in the body. Located in the digestive tracts in the bodies of animals, and in the trap in the bodies of carnivorous plants, they facilitate of food inside<div class="fancy nonEditable" draggable="true"><div class="box4 ui-droppable"></div></div>the cells.  
<p></p>
</div>

<div id="qlist2">
                  <div id="di21-container" class="drag-container">
      <div id="di21" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
        cccc      </div>
    </div>

    <div id="di22-container" class="drag-container">
      <div id="di22" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
         bbbb      </div>
    </div>

    <div id="di23-container" class="drag-container">
       <div id="di23" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
           aaaa       </div>
    </div>
     <div id="di24-container" class="drag-container">
       <div id="di24" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
           eeee       </div>
    </div>
     <div id="di25-container" class="drag-container">
       <div id="di25" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
           iiii       </div>
    </div>
     <div id="di26-container" class="drag-container">
       <div id="di26" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
           dddd       </div>
    </div>
      <div id="di27-container" class="drag-container">
       <div id="di27" class="qitem5 di1 ui-draggable ui-draggable-handle" style="position: relative;">
           ffff       </div>
    </div>
                                                </div>

Above is the code for draggable and droppable. If I set some words as default inside div box then drag and drop will be collapsed why is it? If anyone knows solution please help me. Thanks in advance. The working https://jsfiddle.net/c62tpc45/2/

Upvotes: 0

Views: 913

Answers (1)

Rahul
Rahul

Reputation: 18577

Try this code,

   var oldMouseStart = $.ui.draggable.prototype._mouseStart;
$.ui.draggable.prototype._mouseStart = function(event, overrideHandle, noActivation) {
  this._trigger("beforeStart", event, this._uiHash());
  oldMouseStart.apply(this, [event, overrideHandle, noActivation]);
};

function makedraggable3() {
  $(".qitem5").draggable({
    beforeStart: function() {
            window.source_index = $(this).attr("data-index");
      window.source_html = $(this).html();
    },
    "revert": "invalid"
  });
  $(".box4").droppable({
    "accept": ".qitem5",
    classes: {
      "ui-droppable-active": "ui-state-active",
      "ui-droppable-hover": "ui-state-hover"
    },
    "drop": function(event, ui) {
      window.dest_html = $(this).html();
      window.dest_index = $(this).attr("data-index");
      if (typeof window.dest_html != 'undefined' && window.dest_html != '') {
        switchContent();
      } else {
        var $presentChild = $(this).find(".qitem5"),
          currChildId = $presentChild.attr("id"),
          $currChildContainer = $("#" + currChildId + "-container");
        $currChildContainer.append($presentChild);
        $presentChild.removeAttr("style").removeClass("drag-center-in-droppable");
        makedraggable3();
        $(ui.draggable).clone().appendTo(this).removeAttr("style").addClass("drag-center-in-droppable");
        $(ui.draggable).remove();
      }
    }
  });
}
makedraggable3();
function switchContent() {
  var source_html = $("<div>").addClass("ui-widget-header").addClass("box4").attr("data-index", window.dest_index).append(window.source_html);
  var dest_html = $("<div>").addClass("qitem5").addClass("di1").attr("data-index", window.source_index).append(window.dest_html);

  $("[data-index=" + window.dest_index + "]").closest(".drag-container").html(source_html);
  $("[data-index=" + window.source_index + "]").closest(".drag-container").html(dest_html);
  $(":ui-draggable").draggable("destroy");
  makedraggable3();
}

You just need to map your html as

 <div>
  Lorem Ipsum is simply
  <div class="drag-container">

    <div class="box4" data-index='drop-1'>Hiii</div>
  </div>
  text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy
  <div class="drag-container">
    <div class="box4" data-index='drop-2'></div>
  </div> ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised
  in the 1960s with the release of Letraset sheets containing
  <div class="drag-container">
    <div class="box4" data-index='drop-3'></div>
  </div> passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<hr/>
<div>
  <div id="di21-container" class="drag-container">
    <div id="di21" class="qitem5 di1" data-index="drag-1">
      dummy
    </div>
  </div>
  <div id="di21-container" class="drag-container">
    <div id="di22" class="qitem5 di1" data-index="drag-2">
      textts
    </div>
  </div>
  <div id="di21-container" class="drag-container">
    <div id="di23" class="qitem5 di1" data-index="drag-3">
      woww
    </div>
  </div>
</div>

Give it a try, this should work.

Here is working jsfiddle

Upvotes: 1

Related Questions