Accessing the `draggable` attribute using javascript

So the question is: how can you do this? I want to change the attribute draggableto "false", so an element that was previously dragged will lose this property.

I will give you a div so that you have a place to start:

     <div id="div1"  draggable="true" ondragstart="drag(event) "onDblClick="edit('div1')">
     </div>
+5
source share
4 answers
document.getElementById('div1').setAttribute('draggable', false);

OR

var elem = document.getElementById('div1');
elem.setAttribute('draggable', false);

If you want the attribute to completely disappear,

elem.removeAttribute('dragable');
+6
source

There are many ways if you use jQuery:

    $('#div1').attr('dragabble'); //returns with the value of attribute
    $('#div1').attr('dragabble','false'); //sets the attribute to false

Native JS:

    document.getElementById('div1').getAttribute('draggable'); //returns the value of attr
    document.getElementById('div1').setAttribute('draggable', 'false'); //set the value of attr
+3
source

, jquery-ui draggable ur. jquery ur Dev,

$( "# div1" ) ( '', );.

javascript,

document.getElementById ('div1'). setAttribute ('draggable', 'false');

+2
source

After setting the attribute draggableto trueyou most often want to handle the event dragstart. To do this, in native JS:

elem.addEventListener('dragstart', ev => {
  // dragstart handlings here
  }, false)
0
source

All Articles