0

I have built a sort of picture gallery in JS. On given img and p elements, the onmousedown events calls two functions: it loads a new image and a new description (see below)

But, as opposed to what I was thinking, the onmousedow it is triggered by right-click too. I don't like this and I'd love to limite the onmousedown event to the left-click of the mouse.

Is there any way to do this?

Many thanks for your help.

HTML:

<img style="max-width: 100%; cursor:pointer;" src="../../img/picture1.jpg" alt="text" title="click me" 
id="showgallery" onmousedown="changeimg(); changedscrpt()" />
enter code here
<p class="myclass" style="text-align: center;" id="imgdescription">Description</p>

1 Answers1

0

You should move out calls to changeimg() and changedscrpt() to separate method which would evaluate first whether left mouse was pressed, something like that:

onmousedown="checkAndChange(event, this)";

JavaScript:

function checkAndChange(e, obj)
{
    if (leftMousePressed(e))
    {
        changeimg();
        changedscrpt();
    }
}
function leftMousePressed(e)
{
    e = e || window.event;
    var button = e.which || e.button;
    return button == 1;
}

If the right mouse button will be pressed, JavaScript will not do anything (will not change next image).

  • I tried out your script, it does recognize whether the click is left or right but imgs and descriptions don't load anymore:) – Digital Feb 18 '13 at 06:44