{"id":326,"date":"2013-10-08T22:00:09","date_gmt":"2013-10-08T14:00:09","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=326"},"modified":"2018-09-29T22:20:03","modified_gmt":"2018-09-29T14:20:03","slug":"how-to-use-jquery-to-detect-mouse-clicks","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-detect-mouse-clicks\/","title":{"rendered":"How to use jQuery to detect mouse clicks"},"content":{"rendered":"<p>Right after I found out <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves\/\" title=\"How to use jQuery to get mouse cursor coordinates when mouse moves\" target=\"_blank\">how to get mouse cursor coordinates when mouse moves<\/a>, I went ahead with exploring how to detect mouse clicks.<\/p>\n<p>This post discuss how I had used jQuery to detect mouse clicks.<\/p>\n<h2>Registering a JavaScript callback function to listen to mouse clicks<\/h2>\n<p>Getting my codes notified of mouse clicks is simple via jQuery:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$(document).mousedown(function(e) {\r\n    switch(e.which) {\r\n        case 1:\r\n\t    $('#mouseButtonStatus')\r\n                .html(&quot;You had just clicked on the left button of your mouse&quot;);\r\n            break;\r\n        case 2:\r\n\t    $('#mouseButtonStatus')\r\n                .html(&quot;You had just clicked on the scroll button of your mouse&quot;);\r\n\tbreak;\r\n        case 3:\r\n\t    $('#mouseButtonStatus')\r\n                .html(&quot;You had just clicked on the right button of your mouse&quot;);\r\n            break;\r\n    }\t\t\t\r\n});\r\n<\/pre>\n<p>I wrap the document object with <code>jQuery<\/code> and supplied a function to the <code>mousedown<\/code> function. In the function, I will receive a event object <code>e<\/code> from jQuery when mouse clicks are detected. I then use <code>e.which<\/code> to switch between numbers that identify which mouse button was being clicked. A 1 will mean that the left button of my mouse was clicked, a 2 will mean that the scroll button of my mouse was clicked and a 3 will mean that the right button of my mouse was clicked.<\/p>\n<p>In each of the three cases, I then use <code>jQuery<\/code> to look up the HTML element labeled with <code>mouseButtonStatus<\/code> as its id and inject the appropriate messages as its HTML content.<\/p>\n<h2>The entire HTML document<\/h2>\n<p>To fully realize this proof of concept, I head over to <a href=\"http:\/\/www.techcoil.com\/blog\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">my list of HTML code segments<\/a> and construct a HTML document. In the head section, I add the jQuery codes; In the body, a HTML div element with <code>mouseButtonStatus<\/code> as its id to contain the message.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;script type=&quot;text\/javascript&quot; src=&quot;\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.7.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n\t$(document).ready(function(){\t   \r\n\t    $(document).mousedown(function(e) {\r\n\t        switch(e.which) {\r\n\t\t    case 1:\r\n\t\t        $('#mouseButtonStatus')\r\n                            .html(&quot;You had just clicked on the left button of your mouse&quot;);\r\n\t\t    break;\r\n\t\t    case 2:\r\n\t\t\t$('#mouseButtonStatus')\r\n                            .html(&quot;You had just clicked on the scroll button of your mouse&quot;);\r\n\t\t    break;\r\n\t\t    case 3:\r\n\t\t\t$('#mouseButtonStatus')\r\n                            .html(&quot;You had just clicked on the right button of your mouse&quot;);\r\n\t\t    break;\r\n\t\t}\t\t\t\r\n\t    });\r\n\t});\r\n    &lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div id=&quot;mouseButtonStatus&quot;&gt;\r\nNo mouse clicks detected yet.\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<div class=\"callToAction\">\n   <a target=\"_blank\" href=\"http:\/\/www.techcoil.com\/proof-of-concepts\/using-jquery-to-detect-mouse-clicks\" class=\"readMoreLink\">See this proof of concept<\/a>\n<\/div>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-5g\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5g&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5g&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F326&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Right after I found out <a href=\"http:\/\/www.techcoil.com\/blog\/how-to-use-jquery-to-get-mouse-cursor-coordinates-when-mouse-moves\/\" title=\"How to use jQuery to get mouse cursor coordinates when mouse moves\" target=\"_blank\">how to get mouse cursor coordinates when mouse moves<\/a>, I went ahead with exploring how to detect mouse clicks.<\/p>\n<p>This post discuss how I had used jQuery to detect mouse clicks.<\/p>\n","protected":false},"author":1,"featured_media":1208,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[16,128,89,171],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jquery-logo-mark-dark.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5g","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/326"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=326"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/326\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1208"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}