Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: preventDefault() not working on input/click events?
    primarykey
    data
    text
    <p>I want to disable the default contextMenu when a user right-clicks on an input field so that I can show a custom contextMenu. Generally speaking, its pretty easy to disable the right-click menu by doing something like:</p> <pre><code>$([whatever]).bind("click", function(e) { e.preventDefault(); }); </code></pre> <p>And in fact, I can do this on just about every element EXCEPT for input fields in FF - anyone know why or could point me towards some documentation?</p> <p>Here is the relevant code I am working with, thanks guys.</p> <p>HTML:</p> <pre><code>&lt;script type="text/javascript"&gt; var r = new RightClickTool(); &lt;/script&gt; &lt;div id="main"&gt; &lt;input type="text" class="listen rightClick" value="0" /&gt; &lt;/div&gt; </code></pre> <p>JS:</p> <pre><code>function RightClickTool(){ var _this = this; var _items = ".rightClick"; $(document).ready(function() { _this.init(); }); this.init = function() { _this.setListeners(); } this.setListeners = function() { $(_items).click(function(e) { var webKit = !$.browser.msie &amp;&amp; e.button == 0; var ie = $.browser.msie &amp;&amp; e.button == 1; if(webKit||ie) { // Left mouse...do something() } else if(e.button == 2) { e.preventDefault(); // Right mouse...do something else(); } }); } } // Ends Class </code></pre> <p><strong>EDIT:</strong></p> <p>Sorry, after reading the comments I realize that I should clarify a few things. </p> <p>1) The code above does work...in a sense. The code is able to sort through which button was clicked, it just doesn't care that I say e.preventDefault() and the right-click menu still pops up. In other words, if you put an alert on e.button you would get your 1 or 0 for left and 2 for right...but it just laughs at me and still shows the damned default menu!</p> <p>2) If I put the jQuery selector on ANY other element (other than input) then everything will work, FF will respect the preventDefault() call and the default right-click menu will not show.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload