{"id":55,"date":"2014-02-28T11:53:00","date_gmt":"2014-02-28T09:53:00","guid":{"rendered":"http:\/\/en.nisi.ro\/blog\/?p=55"},"modified":"2014-07-13T19:39:37","modified_gmt":"2014-07-13T17:39:37","slug":"detect-left-mouse-click-right-mouse-click-jquery","status":"publish","type":"post","link":"https:\/\/en.nisi.ro\/blog\/development\/javascript\/detect-left-mouse-click-right-mouse-click-jquery\/","title":{"rendered":"How to detect left mouse click and right mouse click with jQuery"},"content":{"rendered":"<p>If you want to implement a javascript interface probably you will get to a point where you have to know witch button was pressed. The next code will detect correctly for IE, Firefox, Chrome, Opera and Safari.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\n\/* this will append click detection to whole document\r\n * if you want to add this feature just on one area\r\n * just change the selector for the jQuery\r\n *\/\r\njQuery(document).click(function(event) {\r\n   if (jQuery.browser.msie){\r\n      if (event.which &lt; 2){\r\n         alert('Left button');\r\n      }\r\n   }else\r\n   switch (event.which) {\r\n      case 1:\r\n         alert('Left button');\r\n         break;\r\n      case 2:\r\n         alert('Middle button');\r\n         break;\r\n      case 3:\r\n         alert('Right button');\r\n         break;\r\n      default:\r\n         alert('unknown');\r\n   }\r\n\r\n});\r\n\r\n<\/pre>\n<p><!--more--><br \/>\nabove script work ok on Firefox and detect Left Mouse click in Chrome and IE, however Right mouse click is not detected in Chrome\/IE because context menu is fired first.<\/p>\n<p>To disable context menu just add the next code. Do not forget to add selector for the area you need it. And is better to add the right click code to this function instead the above one.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\njQuery(document).bind('contextmenu', function(e){\r\n   e.preventDefault();\r\n   console.log(&quot;Right button&quot;);\r\n   return false;\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to implement a javascript interface probably you will get to a point where you have to know witch button was pressed. The next code will detect correctly for IE, Firefox, Chrome, Opera and Safari. \/* this will append click detection to whole document * if you want to add this feature just [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[46,67,66,65],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript-2","tag-jquery","tag-left-mouse-click","tag-right-mouse-click"],"_links":{"self":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":2,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}