I'm new in jQuery and I would like to create a drag and drop HTML builder.
I have 2 divs: in the first I have pictures that represents HTML blocks in the other div I would like to have a sortable list with the divs that corresponds to the dropped picture.
I've tried a lot of things but It doesn't work. Here is one of multiple parts of code that I've tried :
<html>
<head>
<meta charset="utf-8">
<title>Drag and Drop tests</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
</head>
<body>
<style>
#div_1{height:200px;width:400px;border:5px dotted red;}
#div_2{height:200px;width:400px;background-color:yellow;}
#left{
width:400px;
min-height:400px;
max-height:;
float:left;
border:1px solid black;
margin:0;padding:0;
}
#right{
width:420px;
float:right;
border: 1px solid red;
margin:0;
padding:0;
}
</style>
<script>
$(function(){
$(function(){
$("#left").sortable({
revert: true
});
});
$(function(){
$(".my_div").draggable({
connectToSortable: "#left",
helper: "clone",
stop: function(e, ui){
$("#left").clone().append($(".bloc[data-id="+$(this).attr("data-id")+"]"));
}
});
});
$("div").disableSelection();
});
</script>
<div class="bloc" id="div_1" data_id="1"></div>
<div class="bloc" id="div_2" data-id="2"></div>
<div id="wrap" style="width:1000px;margin:auto">
<div id="left">
</div>
<div id="right" >
<table width="100%" style="text-align:center;height:100%">
<tbody>
<tr style="height:133px">
<td>
<div style="height:100%;width:100%;" class="my_div" data-type="content" data-id="1">
<img src="_pictures/1.png" alt="héhé" />
</div>
</td>
<td>
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="2">
<img src="_pictures/2.png" alt="héhé" />
</div>
</td>
<td>
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="3">
<img src="_pictures/3.png" alt="héhé" />
</div>
</td>
</tr>
<tr style="height:133px">
<td>
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="4">
<img src="_pictures/4.png" alt="héhé" />
</div>
</td>
<td>
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="5">
<img src="_pictures/5.png" alt="héhé" />
</div>
</td>
<td>
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="6">
<img src="_pictures/6.png" alt="héhé" />
</div>
</td>
</tr>
<tr style="height:133px">
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="7">
<img src="_pictures/7.png" alt="héhé" />
</div>
</td>
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="8">
<img src="_pictures/8.png" alt="héhé" />
</div>
</td>
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="9">
<img src="_pictures/9.png" alt="héhé" />
</div>
</td>
</tr>
<tr style="height:133px">
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="10">
<img src="_pictures/10.png" alt="héhé" />
</div>
</td>
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="11">
<img src="_pictures/11.png" alt="héhé" />
</div>
</td>
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="12">
<img src="_pictures/12.png" alt="héhé" />
</div>
</td>
</tr>
<tr style="height:133px">
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="13">
<img src="_pictures/13.png" alt="héhé" />
</div>
</td>
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="14">
<img src="_pictures/14.png" alt="héhé" />
</div>
</td>
<td class="my_td_parent">
<div style="height:100%;width:100%" class="my_div" data-type="content" data-id="15">
<img src="_pictures/15.png" alt="héhé" />
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
Or, if you prefer : https://jsfiddle.net/m2Lnzr1m/