user26697
user26697

Reputation: 117

Issue in Dragging and Dropping elements into TEXT BOX on HTML page?

I want to drag and drop element form a Div to another Div's textbox.

As shown in the above image, there is div1 and div2 from which i have to drag the text/word and drop to the TEXT BOX in DIV2

what is the way to create this function into html page of my project?

EDIT :: 18_7_2014

After implementing Pranav's solution my code like below

 <head>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>

<script>

$(document).ready(function() {
    $(".draggable").draggable({
        revert: true,
        helper: 'clone',
        start: function(event, ui) {
            $(this).fadeTo('fast', 0.5);
        },
        stop: function(event, ui) {
            $(this).fadeTo(0, 1);
        }
    });

    $("#droppable").droppable({
        drop: function(event, ui) {
            this.value = $(ui.draggable).text();
        }
    });
});


</script>

</head>

 <body>

 <div id="sub_cag" style="display:none; margin-left:35px; margin-top:25px; font-family:Arial, Helvetica, sans-serif;">


<span class="draggable"   >sub</span><br>

 <span class="draggable" >sub</span><br>

 <span class="draggable"   >sub</span><br>

 <span class="draggable" >sub</span><br>

 <span class="draggable"   >sub</span><br>

 <span class="draggable" >sub</span><br>

 </div>   

 <div id="droppableHolder" style=" height:400px; width:580px; margin:10px; ">

 <div style="height: 250px; width:580px; margin:0px;">

<div style=" height:60px; width:250px; margin-top:50px; float:; margin-left:0px;">

 <label for="departmant_name" style="margin-left:0px; font-size: 12px;;font-weight:500;font-family:Arial, Helvetica, sans-serif;">Department Name</label>
 <input  type="text" maxlength="25"  id="droppable" style="width:200px;float:left;font-size: 12px;font-size: 12px;height: 15px; border-radius:5px; border: solid #dddddd 2px; font-size:12px;">
</div>

<div style=" height:60px; width:250px; margin-top:0px; float:left; margin-left:0px;">

 <label for="discription" style="margin-left:0px; font-size: 12px;;font-weight:500;font-family:Arial, Helvetica, sans-serif;">Discription</label>
 <input  type="text" maxlength="25"  id="droppable" style="width:200px;font-size: 12px;font-size: 12px;height: 40px; border-radius:5px; border: solid #dddddd 2px; font-size:12px;">
</div>

<div style=" height:60px; width:220px; margin-top:-60px; float:right; margin-left:0px;">

 <label for="sub_department" style="margin-left:0px; font-size: 12px;;font-weight:500;font-family:Arial, Helvetica, sans-serif;">Sub Department</label>
 <input  type="text" maxlength="25" id="droppable"  style="width:200px;font-size: 12px;font-size: 12px;height: 15px; border-radius:5px; border: solid #dddddd 2px; font-size:12px;">
 <button value="ADD"   style="height:20px; width:40px; margin-top:px;"> </button>
</div>

</body>

When browser is trying to execute this line $(document).ready(function(). this "$ is not defined" error is generated by console.

Upvotes: 3

Views: 1989

Answers (3)

Twix
Twix

Reputation: 392

You need to add "jquery.min.js" script file also in your code.

Upvotes: 0

Sankar Jay
Sankar Jay

Reputation: 196

In the above answer, they are using Id element for droppable div section.Id is a Unique one, so we replace this with Class it will helpful to drop to more div boxes as per your requirement.

Here is my updated code

$(function() {
 $(".draggable").draggable({
    revert: true,
    helper: 'clone',
    start: function(event, ui) {
        $(this).fadeTo('fast', 0.5);
    },
    stop: function(event, ui) {
        $(this).fadeTo(0, 1);
    }
});

$(".droppable").droppable({
    drop: function(event, ui) {
        this.value = $(ui.draggable).text();
    }
 });
});

Fiddle: http://jsfiddle.net/5DCZw/942/

Upvotes: 1

Pranav
Pranav

Reputation: 666

I think you are looking to something like this :

$(function() {
    $(".draggable").draggable({
        revert: true,
        helper: 'clone',
        start: function(event, ui) {
            $(this).fadeTo('fast', 0.5);
        },
        stop: function(event, ui) {
            $(this).fadeTo(0, 1);
        }
    });

    $("#droppable").droppable({
        drop: function(event, ui) {
            this.value = $(ui.draggable).text();
        }
    });
});

Fiddle : http://jsfiddle.net/5DCZw/941/

Upvotes: 1

Related Questions