Tom Gullen Tom Gullen - 2 years ago 215
Javascript Question

Javascript ondrag, ondragstart, ondragend

I've been trying to use

and some other functions on a
rendered dynamically on an

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?

Answer Source

Have a look at this documentation:

Note: It does not work in every browser.

If you want a cross-browser support use jQuery:

jQuery example:

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

    $(function() {
        $( "#draggable" ).draggable({
            drag: function(event, ui) {}

Another example:

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

document.getElementById('toBeDragged').addEventListener('dragstart', function(event) {
    event.dataTransfer.setData('text/plain', 'This text may be dragged');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download