jQuery: preventDefault() not working on input/click events?

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-04-29T18:01:00Z Indexed on 2010/04/29 18:07 UTC
Read the original article Hit count: 337

Filed under:
|
|
|

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:

$([whatever]).bind("click", function(e) { e.preventDefault(); });

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?

Here is the relevant code I am working with, thanks guys.

HTML:

<script type="text/javascript">
 var r = new RightClickTool();
</script>

<div id="main">
  <input type="text" class="listen rightClick" value="0" />  
</div>

JS:

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 && e.button == 0;
  var ie = $.browser.msie && e.button == 1;

  if(webKit||ie)
  { 

   // Left mouse...do something()

  } else if(e.button == 2) {
   e.preventDefault(); 

   // Right mouse...do something else();
  }

 });
}

} // Ends Class

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about events