Access the `draggable` attribute with javascript

Tbi45 picture Tbi45 · Apr 4, 2013 · Viewed 19.9k times · Source

So the question is : How can you do that ? I want to change the draggable attribute to "false" so the element that was previously draggable will lose this property.

I will give you a div so you will have something to start with :

     <div id="div1"  draggable="true" ondragstart="drag(event) "onDblClick="edit('div1')">
     </div>

Answer

user1467267 picture user1467267 · Apr 4, 2013
document.getElementById('div1').setAttribute('draggable', false);

OR

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

If you want the attribute totally gone use;

elem.removeAttribute('dragable');