252

I need to put an image in my page. I want to disable dragging of that image. I am trying lot of things but no help. Can somebody help me out ?

I don't want to keep that image as a background-image because I am resizing the image.

Puru
  • 8,913
  • 26
  • 70
  • 91
  • 17
    @AgentConundrum - There is no problem for me if the user saves and does whatever he wants. My only requirement is to not to drag that image. – Puru Nov 18 '10 at 08:45

26 Answers26

294

You can like this...

document.getElementById('my-image').ondragstart = function() { return false; };

See it working (or not working, rather)

It seems you are using jQuery.

$('img').on('dragstart', function(event) { event.preventDefault(); });
alex
  • 479,566
  • 201
  • 878
  • 984
  • 10
    @alex - The purpose of not have image dragging is not stealing the image. The purpose is completely different. I am making that image as sortable and droppable. So it takes a long time to explain it. – Puru Nov 18 '10 at 05:29
  • 2
    @alex - Is ondragstart is browser independent ? – Puru Nov 18 '10 at 05:34
  • @Multiplexer It seems to be except for < Firefox 3.5 – alex Nov 18 '10 at 05:37
  • 1
    @AdamMerrifield Try `$(document)` instead of `$(window)` – rybo111 Jun 21 '16 at 12:55
  • 2
    I just worked out you can simplify the jQuery to: `$('img').on('dragstart', false);` – rybo111 Jun 21 '16 at 12:59
  • @rybo111 Perhaps you could, but I would avoid it as a) AFAIK it's not documented by jQuery and b) confusing to what it would actually be doing. – alex Jun 22 '16 at 08:21
  • 2
    @alex Under handler: [`The value false is also allowed as a shorthand for a function that simply does return false.`](https://api.jquery.com/on/) – Adam Merrifield Jun 22 '16 at 14:35
  • 1
    @AdamMerrifield Well there you go. One thing to note is it will prevent propagation too, which could be a problem. – alex Jun 22 '16 at 14:37
211

CSS only solution: use pointer-events: none

https://developer.mozilla.org/en-US/docs/CSS/pointer-events

mikhuang
  • 2,694
  • 2
  • 19
  • 17
201

just add draggable="false" to your image tag:

<img draggable="false" src="image.png">

IE8 and under doesn't support however.

dmo
  • 5,102
  • 3
  • 25
  • 28
50
window.ondragstart = function() { return false; } 
alex
  • 479,566
  • 201
  • 878
  • 984
Ben
  • 54,723
  • 49
  • 178
  • 224
  • 4
    +1 Don't understand the downvotes. It's not the ultimate solution, while it prevents dragging all, not only a special image. But it shows the right direction(and I think it was the first answer which does it) – Dr.Molle Nov 18 '10 at 05:49
  • @DrMolle It may have been in response to the (since deleted) comments left by Steve on [my answer](http://stackoverflow.com/posts/4211930/edit). – alex Nov 18 '10 at 05:59
  • 2
    Remember that sometimes people will drag a link to their bookmarks bar -- this solution would remove this ability. – jClark Dec 06 '12 at 15:16
  • Shows how to do it, sure, but it prevents dragging everything which is eh.. Upvote anyway, for showing how to do it. – asciidude Oct 16 '21 at 23:48
45

simplest cross browser solution is

<img draggable="false" ondragstart="return false;" src="..." />

problem with

img {
 -moz-user-select: none;
 -webkit-user-select: none;
 -ms-user-select: none;
 user-select: none;
 -webkit-user-drag: none;
 user-drag: none;
 -webkit-touch-callout: none;
}

is that it is not working in firefox

Imal Hasaranga Perera
  • 9,683
  • 3
  • 51
  • 41
30

I tried myself and found this is working.

$("img").mousedown(function(){
    return false;
});

I am sure this disables dragging of all the images. Not sure it effects something else.

Puru
  • 8,913
  • 26
  • 70
  • 91
26
img {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
  user-drag: none;
  -webkit-touch-callout: none;
}

I used it on my website at http://www.namdevmatrimony.in/ It's works like a magic!!! :)

20

You can use inline code for this

<img draggable="false" src="http://www.ourkanpur.com/images/logo.png">

And the second option is use external or on-page css

img {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
  user-drag: none;
  -webkit-touch-callout: none;
}
<img src="http://www.ourkanpur.com/images/logo.png">

Both are Working Correctly I m using external css on this site (Click Here)

Abhishek kamal
  • 221
  • 2
  • 6
16

See this answer; in Chrome and Safari you can use the following style to disable the default dragging:

-webkit-user-drag: auto | element | none;

You could try user-select for Firefox and IE(10+):

-moz-user-select: none | text | all | element
-ms-user-select: none | text | all | element
Community
  • 1
  • 1
mhu
  • 17,720
  • 10
  • 62
  • 93
13

You can add the following to each image you don't want to be draggable, (inside the img tag):

onmousedown="return false;"

e.g.

img src="Koala.jpg" onmousedown="return false;"
bool.dev
  • 17,508
  • 5
  • 69
  • 93
BOBO
  • 131
  • 1
  • 3
13

Directly use this: ondragstart="return false;" in your image tag.

<img src="http://image-example.png" ondragstart="return false;"/>

If you have multiple images, wrapped on a <div> tag:

<div ondragstart="return false;">
   <img src="image1.png"/>
   <img scr="image2.png"/>
</div>

Works in all major browsers.

Jan
  • 131
  • 1
  • 5
11

This code does exactly what you want. It prevents the image from dragging while allowing any other actions that depend on the event.

$("img").mousedown(function(e){
    e.preventDefault()
});
Mo.
  • 26,306
  • 36
  • 159
  • 225
Robert
  • 111
  • 1
  • 2
9

Since my images were created using ajax, and therefore not available on windows.load.

$("#page").delegate('img', 'dragstart', function (event) { event.preventDefault(); });

This way I can control which section blocks the behavior, it only uses one event binding and it works for future ajax created images without having to do anything.

With jQuery new on binding:

$('#page').on('dragstart', 'img', function(event) { event.preventDefault(); }); (thanks @ialphan)

guzart
  • 3,700
  • 28
  • 23
  • 1
    Using .on it will be like this: $(document).on('dragstart','img',function(e){e.preventDefault();}); – ialphan Oct 02 '12 at 20:46
  • This is the correct solution. One event binding for unlimited `img` elements, present or future. Least intensive, most robust. – bearfriend Apr 29 '13 at 14:38
8
<img draggable="false" src="images/testimg1.jpg" alt=""/>
Mustafa
  • 1,738
  • 2
  • 24
  • 34
4

Well I don't know if the answers in here have helped everyone or not, but here's a simple inline CSS trick which would definitely help you to disable dragging and selecting texts on a HTML page.

On your <body> tag add ondragstart="return false". This will disable dragging of images. But if you also want to disable text selection then add onselectstart="return false".

The code will look like this: <body ondragstart="return false" onselectstart="return false">

4

You may consider my solution the best. Most of answers are not compatible with old browsers like IE8 since e.preventDefault() wont be supported as well as ondragstart event. To do it cross browser compatible you have to block mousemove event for this image. See example below:

jQuery

$("#my_image").mousemove( function(e) { return false } ); // fix for IE
$("#my_image").attr("draggable", false); // disable dragging from attribute

without jQuery

var my_image = document.getElementById("my_image");
my_image.setAttribute("draggable", false);

if (my_image.addEventListener) {
   my_image.addEventListener("mousemove", function(e) { return false });
} else if (my_image.attachEvent) {
   my_image.attachEvent("onmousemove", function(e) { return false });
}

tested and worked even for IE8

CodeGems
  • 549
  • 4
  • 16
4

Set the following CSS properties to the image:

user-drag: none; 
user-select: none;
-moz-user-select: none;
-webkit-user-drag: none;
-webkit-user-select: none;
-ms-user-select: none;
Omid Farvid
  • 785
  • 7
  • 13
4

jQuery:

$('body').on('dragstart drop', function(e){
    e.preventDefault();
    return false;
});

You can replace the body selector with any other container that children you want to prevent from being dragged and dropped.

Lis
  • 555
  • 4
  • 26
4

Great solution, had one small issue with conflicts, If anyone else has conflict from other js library simply enable no conflict like so.

var $j = jQuery.noConflict();$j('img').bind('dragstart', function(event) { event.preventDefault(); });

Hope this helps someone out.

Paul
  • 57
  • 1
  • 1
3
document.getElementById('#yourImageId').addEventListener('dragstart', function(e) {
     e.preventDefault();
});

Works in this Plunker http://plnkr.co/edit/HbAbJkF0PVLIMjTmEZml

jbx
  • 21,365
  • 18
  • 90
  • 144
dtothefp
  • 1,364
  • 1
  • 16
  • 22
3

Well, this is possible, and the other answers posted are perfectly valid, but you could take a brute force approach and prevent the default behavior of mousedown on images. Which, is to start dragging the image.

Something like this:

window.onload = function () {  
    var images = document.getElementsByTagName('img');   
    for (var i = 0; img = images[i++];) {    
        img.ondragstart = function() { return false; };
    }  
};  
yckart
  • 32,460
  • 9
  • 122
  • 129
Alex
  • 64,178
  • 48
  • 151
  • 180
2

Answer is simple:

<body oncontextmenu="return false"/> - disable right-click <body ondragstart="return false"/> - disable mouse dragging <body ondrop="return false"/> - disable mouse drop

Gain
  • 29
  • 1
2

An elegant way to do it with JQuery

$("body").on('mousedown','img',function(e){
    e.stopPropagation();
    e.preventDefault();
});

the on function attachs an event handler function for one or more events to the selected elements

ucMedia
  • 4,105
  • 4
  • 38
  • 46
0

I did the css properties shown here as well as monitor ondragstart with the following javascript:

handleDragStart: function (evt) {
    if (evt.target.nodeName.match(/^(IMG|DIV|SPAN|A)$/i)) {
      evt.preventDefault();
      return false;
    }
  },
Alocus
  • 1,860
  • 3
  • 21
  • 32
0

Simply use e.preventDefault(). Examples:

Pure HTML:

<img
  src={item.icon}
  onmousedown={(e) => {
    e.preventDefault()
  }}
/>

React

<img
  src={item.icon}
  onMouseDown={(e) => {
    e.preventDefault()
  }}
/>
Duc Trung Mai
  • 2,141
  • 1
  • 24
  • 23
-1

Stealing from my own answer, just add a completely transparent element of the same size over the image. When you resize your image, be sure to resize the element.

This should work for most browsers, even older ones.

Community
  • 1
  • 1
Sablefoste
  • 4,032
  • 3
  • 37
  • 58