NorthLake
NorthLake

Reputation: 75

Disable dragging for all images

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

Answers (2)

suchislife
suchislife

Reputation: 1

How about...

document.getElementsByClassName("class_values");

How to “get” elements

Upvotes: 0

suchislife
suchislife

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

Related Questions