Search code examples
javascriptdraggable

JS Disable drag and drop (draggable attribute)


Using this method, I have some elements on my page that can be dragged from one column to another.

I would like to be able to make the drag permanent. That is, once an element is dragged (or dropped I guess) ONCE, it can no longer be dragged.

I tried setting draggable to false using setAttribute, but that didn't seem to work.

So how could you (using plain old JS, no jQuery stuff) disable the draggable ability of an element?

I actually searched this and only found stuff about disabling dragging and dropping of files and other stuff onto a page, that is not what I am asking about. Thanks in advance.


Solution

  • On the W3Schools page you referenced - past this into the console:

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

    Then try to drag the element. I think you'll find it works ;)

    If you provide your code, I will help you debug.