I'm trying to perform two image swapping when a user clicks on an image and drags the other image.
However, my code does not put the images in the correct position, it leaves the destination image in the same place and puts the image that was dragged in <div>
following, overlapping another image.
$(".elemento").draggable();
$(".containerImg").droppable({
drop: function(event, ui) {
var dropped = ui.draggable;
var droppedOn = event.target;
$(dropped).css({top: 0,left: 0}).appendTo(droppedOn);
$(droppedOn.querySelector('img')).css({top: 0,left: 0}).appendTo(dropped.parent());
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><divclass="painel-tabuleiro">
<div class="col-1">
<div class="containerImg"><img class="elemento" src="imagem/1.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/1.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/1.png"/></div>
</div>
<div class="col-2">
<div class="containerImg"><img class="elemento" src="imagem/2.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/2.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/2.png"/></div>
</div>
<div class="col-3">
<div class="containerImg"><img class="elemento" src="imagem/3.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/3.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/3.png"/></div>
</div>
<div class="col-4">
<div class="containerImg"><img class="elemento" src="imagem/4.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/4.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/4.png"/></div>
</div>
</div>