I am unable to disable image dragging when using a <picture>
element. I have used this in the past with an <img>
element with success. I’m not sure why it doesn’t work with <picture>
.
HTML Code:
JavaScript
x
6
1
<picture class="thumb">
2
<source class="nodrag" srcset="image.webp" type="image/webp">
3
<source class="nodrag" srcset="image.gif" type="image/gif">
4
<img class="thumb" src="image.gif">
5
</picture>
6
My Javascript:
JavaScript
1
3
1
document.getElementsByClassName('thumb').ondragstart = function () { return false; };
2
document.getElementsByClassName('nodrag').ondragstart = function () { return false; };
3
The above did not work. Is there another way I can disable dragging of a element to prevent users from dragging to save the image?
Advertisement
Answer
Not sure if it will work for your use-case but have you tried the HTML draggable attribute?
JavaScript
1
2
1
<img class="thumb" src="image.gif" draggable="false">
2