Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Equivalent of .onclick for Right Click?

    What would be the equivalent of .onclick for Right Click?

    For Left Click, this is what I'm using:

    document.onclick = mouseLeftClick;

    function mouseLeftClick(){
    alert("Clicked");
    }


    What would it be for Right Click? It can be via JQuery.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Clicking any of the three mouse buttons triggers the click event. If you want different processing for different buttons then you need to test which button was pressed. See http://javascriptexample.net/events10.php for an example of how to extract the information into three separate fields that contain 1 if that particular button was pressed and 0 if it wasn't. Alternativelyif you test the button property of the event directly then a value of 2,3,6, or 7 indicates that the right button was pressed (just as 1,3,5, or 7 indicates that the left button was pressed and 4,5,6 or 7 indicates that the centre button was pressed).

    Note that some browsers do not pass right mouse clicks to JavaScript as it can interfere with the operation of the context menu.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I'm using Google Chrome and .onclick is only triggered if I use the Left Button.
    Any solution for Chrome?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Here is one way to do it in Chrome:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <div id="demo" style="font-size: xx-large; background-color: pink;">
    this is some text
    </div>
    <script type="text/javascript">
    var div = document.getElementById("demo");
    div.onclick = function() { 
        alert("left click in div");
    }
    div.oncontextmenu = function(event) {
        event.preventDefault();
        event.stopPropagation();
        alert("right click in div");
        return false;
    };
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    IdkWhatsRc (05-31-2013)

  • #5
    New Coder
    Join Date
    May 2013
    Posts
    45
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Working
    Thanks a lot.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by Old Pedant View Post
    Here is one way to do it in Chrome:
    Note that way will not work in Opera unless the owner of the browser has changed the default so as to enable right click detection.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes, you need to detect which browser is in use, of course. I *did* say it is *one way* to do it in Chrome; never claimed more than that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by felgall View Post
    Note that way will not work in Opera unless the owner of the browser has changed the default so as to enable right click detection.
    opera beta is now using webkit (as of this week), so perhaps the clock is ticking on this gotcha...
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •