Reputation: 75
So, I want to disable dragging on all images on a particular page. I've made this work with ondragstart="return false"
, but I hate working with HTML attributes when I can make it work with JQuery (especially since it involves a lot of images). The code I've got is:
const img = document.getElementsByTagName("img")
for (let i = 0; i < img.length; i++) {
img[i].addEventListener("drag", function(a) {
a.preventDefault;
});
}
When I look in the developer tools I can see the event listener on every image, but I can still drag them. What's going on here?
By the way, I've also tried function() {return false}
.
Upvotes: 2
Views: 220
Reputation: 1
How about...
HTML ondragstart Event Attribute
<body>
<img src="html5.png" id="html5" width="256px" height="256px" alt="HTML5" />
</body>
<script>
document.getElementById('html5').ondragstart = function () { return false; };
</script>
Upvotes: 1