I am encountering an issue with dragging and dropping images or swapping them using JavaScript. I have tried to implement this in the code below, where clicking on icons moves them onto a colored div. However, I am now trying to drag and drop the images within the div or even swap them around. I am struggling to achieve this. Can someone please assist me with this task?
Thank you in advance.
This is the code I have:
function changeHome() {
document.getElementById('color').style.backgroundColor = "coral";
}
function changeGray() {
document.getElementById('color').style.backgroundColor = "gray";
}
function changeRed() {
document.getElementById('color').style.backgroundColor = "red";
}
function changeGreen() {
document.getElementById('color').style.backgroundColor = "green";
}
function changeBlue() {
document.getElementById('color').style.backgroundColor = "blue";
}
$(document).ready(function() {
$('.image').click(function() {
var srcimg = $(this).attr('src');
$('#color').append("<img src=" + srcimg + " width='70' height='70'>");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<br/>
<div class="container">
<h1 align="center"><u> JavaScript Simple learning </u></h1>
<h3 align="center"> Use of JavaScript to drag and drop icons in Background image </h3>
<br/> <br/>
<div class="col-md-10">
<div id="color">
</div>
</div>
<div class="col-md-2">
<button onclick="changeHome()"> Home Color </button> <br/><br/><br/>
<button onclick="changeGray()"> Click for Gray </button> <br/><br/>
<button onclick="changeRed()"> Click for Red </button> <br/><br/>
<button onclick="changeGreen()"> Click for Green </button> <br/><br/>
<button onclick="changeBlue()"> Click for Blue </button> <br/><br/>
</div>
</div>
<div class="container">
<h2><u> List of Icons </u></h2>
<img class="image" src="images/fb.jpeg" width="70" height="70" />
<img class="image" src="images/twitter.jpeg" width="70" height="70" />
<img class="image" src="images/linkedin.jpeg" width="70" height="70" />
<img class="image" src="images/gmail.jpeg" width="70" height="70" />
<img class="image" src="images/instagram.jpeg" width="70" height="70" />
<img class="image" src="images/whatsapp.jpeg" width="70" height="70" />
<img class="image" src="images/telegram.jpeg" width="70" height="70" />
</div>