We have answer of your question!

100% solved queries, no empty question

Question: jQuery - *:not() not excluding elements


0

My goal is to disable right-click everywhere except <kbd> and <textarea> tags. I am using *:not(kbd,textarea) to exclude disabling right-click - .on('contextmenu', function(e){ e.preventDefault(); });. It is supposed that I can right click on <kbd> and <textarea> tags but I cannot. It is werid.

$(document).ready(function(){
  $('*:not(kbd,textarea)').on('contextmenu', function(e){
      e.preventDefault();
  });
});
div {
  width: 170px;
  height: 170px;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<kbd>right click me</kbd>
<textarea>right click me</textarea>
can't right click me
</div>
Question author Louis55 | Source

Answer


1


To be clear: *:not(kbd,textarea) works just fine. The problem has to do with how events are processed in browsers.

Most events bubble up, contextmenu does as well. Since the <kbd> and <textarea> elements are inside the <div>, the event handler bound to the <div> will always cancel the contextmenu event.

Depending on your exact use case, you could simply check whether the event originated on the element itself and only prevent it in that case:

$(document).ready(function(){  $('*:not(kbd,textarea)').on('contextmenu', function(e){      if (this === e.target) {        e.preventDefault();      }  });});
div {  width: 170px;  height: 170px;  background-color: green;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div><kbd>right click me</kbd><textarea>right click me</textarea>can't right click me</div>
Answer author Felix-kling

Tickanswer.com is providing the only single recommended solution of the question jQuery - *:not() not excluding elements under the categories i.e jquery , jquery-selectors , . Our team of experts filter the best solution for you.

Related Search Queries:

jquery not this , :not css , jquery not hasclass , jquery not multiple classes , jquery not not working ,


You may also add your answer

Thanks for contributing an answer to Tick Answer!