keepwalking
keepwalking

Reputation: 2654

Disable right click except some elements

I am using this code to disable right click on my page:

$(document).ready(function() {
    $(document).bind("contextmenu",function(e) {
       return false;
    });
}); 

How can i add some exception, lets say when you right click on image the context menu should not be disabled.

Maybe something with stopPropagation but can't figure it out.

Thanks

Upvotes: 4

Views: 1726

Answers (2)

Mr7-itsurdeveloper
Mr7-itsurdeveloper

Reputation: 1631

Try this:

  <script type="text/javascript">
    $(function () { 
      $('#btnClick').bind('contextmenu',function(e){
        e.preventDefault();
        alert('Right Click is not allowed');
      }); 

    });
  </script>



 <input type="text" /><br>
    <input type="checkbox" />

  <button id="btnClick">Click</button>

http://jsfiddle.net/NLJ6t/1/

Upvotes: 0

Jai
Jai

Reputation: 74738

with event.target:

$(document).ready(function() {
  $(document).bind("contextmenu", function(e) {
     if(!$(e.target).is('img')){
        return false;
     }
  });
});

Upvotes: 5

Related Questions