<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
<h2>Drag and Drop</h2>
<p>Drag the image back and forth between the two div elements.</p>
<div class="container-drop">
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="/imagens/img_cod/924081810.gif" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
</div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img src="/imagens/img_cod/924081810.gif" draggable="true" ondragstart="drag(event)" id="drag2" width="88" height="31">
</div>
<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
<div id="result">
</div>
</body>
</html>
#div1, #div2, #div3 {
float: left;
width: 100px;
height: 35px;
margin: 10px;
padding: 10px;
border: 1px solid black;
}
.container-drop {
display: flex;
}
.result {
display: flex;
flex-direction: column;
}
#result div label{
padding: 5px;
}
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
setOrder()
}
function setOrder(){
var el = document.getElementsByClassName('container-drop')[0];
var list = el.getElementsByTagName('div');
var arraySorted = fetchArray(list);
console.log(arraySorted);
setResult(arraySorted);
}
function buildObjectImage(container, image) {
var itemImage = {
container: container,
image: image
};
return itemImage;
}
function fetchArray(list) {
var orderImages = [];
for(var i = 0; i < list.length; i++) {
var boxDropId = list[i].id;
var hasChildren = list[i].children[0];
if(hasChildren) {
var imageId = list[i].children[0].id;
orderImages.push(buildObjectImage(boxDropId, imageId));
} else {
orderImages.push(buildObjectImage(boxDropId, undefined));
}
}
return orderImages;
}
function setResult(arraySorted) {
var result = document.getElementById("result");
result.innerHTML = '';
var label;
for(var i = 0; i < arraySorted.length; i++) {
container = document.createElement("label");
container.innerHTML = arraySorted[i].container;
image = document.createElement("label");
image.innerHTML = arraySorted[i].image;
div = document.createElement("div");
div.append(container);
div.append(image);
result.append(div);
}
}
setOrder();
Output
This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account
Dismiss xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |