'how can we run drag and drop codes with the help of html, css and a bit of javascript? [closed]
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
<script>
function dragstart(ev){
ev.dataTransfer.setData("Text",ev.target.id);
}
function dragfinish(ev){
ev.preventDefault();
}
function dropstart(ev){
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
ev.preventDefault();
}
</script>
</head>
<body>
<p text-style="center"> I started to work on drag and drop</p>
<div style="width:400px;height:380px; border:2px; border-style:solid;" id="div1" ondrop="dropstrat(event)" ondragover="dragfinish(event)"></div>
<div style="height:300></div>
<img id="pic1 src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTAqB_HEXzRtgLQiHn71I1-ph0cAhoiBXkMwA&usqp=CAU" width="355" height="355" draggable="true" ondragstart="dragstart(event)">
</body>
I tried this code but have no idea what is wrong with it? can anyone help me! I wonder whether the problems roots in my web browser, upper case or lowercase, or I don't know maybe a semicolon can make problem? the thing is I can drag an element in my web page, but I cannot drop in in the div that I defined, I guess all went well except the time I wrote script.
Solution 1:[1]
The problem is that your
ondrop="dropstrat(event)"
instead of
ondrop="dropstart(event)"
Solution 2:[2]
A lot of typos, missing quote etc
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
<script>
function dragstart(ev) {
ev.dataTransfer.setData("Text", ev.target.id);
}
function dragfinish(ev) {
ev.preventDefault();
}
function dropstart(ev) {
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
ev.preventDefault();
}
</script>
</head>
<body>
<p text-style="center"> I started to work on drag and drop</p>
<div style="width:400px;height:380px; border:2px; border-style:solid;" id="div1" ondrop="dropstart(event)" ondragover="dragfinish(event)"></div>
<div style="height:300"></div>
<img id=" pic1" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTAqB_HEXzRtgLQiHn71I1-ph0cAhoiBXkMwA&usqp=CAU" width="355" height="355" draggable="true" ondragstart="dragstart(event)" />
</body>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Icekid |
Solution 2 | aleng |