Javascript ondrag, ondragstart, ondragend

I haven't used drag myself much but I believe it's the drag "edit action" - eg selecting text and dragging it within a textbox/textarea.

You may need to implement your own onmousedown() onmouseup() and onmousemove() handlers for the functionality I think you're after


Short answer: <div>-s are not draggable by default, unlike <a>-s. You must add draggable=true:

<div draggable=true ondragstart=...>HTML</div>

Works on Firefox 75 like that.


Have a look at this documentation: https://developer.mozilla.org/En/DragDrop/Drag_and_Drop

Note: It does not work in every browser.

If you want a cross-browser support use jQuery: http://jqueryui.com/demos/draggable/

jQuery example:

<div id="draggable" class="ui-widget-content">
    <p>Drag me around</p>
</div>

<script>
    $(function() {
        $( "#draggable" ).draggable({
            drag: function(event, ui) {}
        });
    });
</script>

Another example:

<div id="toBeDragged" draggable="true">
  This text <strong>may</strong> be dragged.
</div>

<script>
document.getElementById('toBeDragged').addEventListener('dragstart', function(event) {
    event.dataTransfer.setData('text/plain', 'This text may be dragged');
});
</script>