Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript ondrag, ondragstart, ondragend

I've been trying to use ondrag() and some other functions on a div rendered dynamically on an HTML page.

None of these events seem to fire, nor do I get any errors. I can't find much helpful documentation on it either. Am I interpreting it wrongly, can you use these events to script functionality to drag a div around the screen?

like image 904
Tom Gullen Avatar asked Dec 06 '10 13:12

Tom Gullen


1 Answers

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>
like image 117
Tower Avatar answered Oct 06 '22 08:10

Tower