Oztaco
Oztaco

Reputation: 3459

Whats wrong with my mouseup code?

Trying to detect left click vs right click (without using jQuery!) and I have the following code

Javascript:

function cclick(e) {
   if (e.button == 1) {alert("hgbdygwea");}
   else {
      alert(e.button);
   }
}

HTML:

<a onmouseup="cclick()" ...> <img .../> </a> <!-- also tried cclick instead of cclick() -->

using internet explorer 9

Upvotes: 0

Views: 326

Answers (4)

Eric
Eric

Reputation: 6354

I think this code should work for you. e.button is not valid for all browsers (and I cleaned up the code).

function cclick(e) {
    "use strict";
    e || window.event;
    if (e.which) {
        alert((e.which === 1) ? "hgbdygwea" : e.which);
    } else {
        alert((e.button === 1) ? "hgbdygwea" : e.button);
    }
}

<a onmouseup="cclick(event)" ...> <img .../> </a> <!-- also tried cclick instead of cclick() -->

Upvotes: 0

prageeth
prageeth

Reputation: 7395

Following is another approach

function cclick() {
   var e = window.event;
   if (e.button == 1) {alert("hgbdygwea");}
   else {
      alert(e.button);
   }
}

And call the function as below without passing the event.

<a onmouseup="cclick()" ...> <img .../> </a>

Upvotes: 0

nnnnnn
nnnnnn

Reputation: 150040

You need to pass the event object to your function:

onmouseup="cclick(event);"

Upvotes: 3

Marcel
Marcel

Reputation: 28087

Quirksmode has a good write up on the subject of "Which mouse button has been clicked?" and his code works over the top of yours.

Upvotes: 1

Related Questions