M
M
Max Darkleviathan2020-02-25 22:13:51
JavaScript
Max Darkleviathan, 2020-02-25 22:13:51

How to drag DND text to input?

There is this code:

print "<a draggable='true' ondragstart='drag(event)'>Hello</a>";
print "<br><input id=data type=text name=p1 ondrop='drop(event)' ondragover='allowDrop(event)'>"


<script>
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));
}
</script>

Gives such an error.
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
Help me figure out what the problem is.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ibishka, 2020-02-25
@Ibishka

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData('text');
    var elem = document.getElementById(data)
    ev.target.appendChild(elem);
}

M
Max Darkleviathan, 2020-02-26
@darkleviathan

I already solved this problem myself. Thanks to everyone for the help)
I leave the code here just in case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question