views:

61

answers:

5

I have an image in my page and I don't want it to pick up mouse clicks (as in the browsers inherent drag and drop or the drag select where it highlights the image in the browser.)

Is there something I can do to the img or its parent dom element or the page to make the browser not do anything when I click on the image? I need to use the mousedown for something else, but the browser seems to perform my mousedown event as well as do its own dragging or highlighting, and I don't want it to. Any ideas?

+4  A: 

Create a div the same size as the image, and then set the image to be the background of the div.

recursive
I like simple, this sounds great, but I'm trying it and it's no going. I'm making these img/divs dynamically and whenever I try to set the style="background: url(blah)" I get an error trying to set when only a getter available. And if I use style.backgroundImage = url then I get no error, but the image doesn't show.Is there some z index or coloring I have to set in my newly created element or some inline style or something?
stu
never mind I had the syntax wrong, got it working, thanks.
stu
oh heh, new problem. I need to stretch the background image, from what I'm reading, it's not possible. Did I miss some trick?
stu
I ended up doing something simpler if a little goofier.I made a div sandwich. div1 has zindex 1 and all the images are children of div1, div2 is absolute position sized and positioned over div2 with zindex 10.
stu
A: 

Best idea what I get, is to cover <img ... by some transparent <div and bind mousedown action to its :) EDIT: or of course, if u won't play with img, u can use it as a background for your div.

Rin
what do you mean "if you won't play with img" I created the image I can do whatever I want, but I don't want to make two elements if I don't have to.
stu
I meant, if ur img is as background-image, will be harder change it, resize, etc...
Rin
+1  A: 

using jquery,

$('img').click(function() {
  return false;
})
Zane Edward Dockery
It really could be done without that 30KB ;>
Rin
Some people already use jQuery on their sites...
Lèse majesté
+2  A: 

As recursive said, use a div, and set it's background image to the image you want, e.g.:

<div style="width: 879px; height: 576px; background-image: url(image.png);">
Vincent McNabb
A: 

Images are draggable by default. To override the browser default behaviour, try:

<img draggable="false"...
robertc