How to disable clicking inside div

JavascriptJquery

Javascript Problem Overview


For many reasons I need to disable clicking on certain content within a div element. Due to clicking ads attacks, for example. I still want it to be visible tho.

Consider the following snippet :-

<div class="ads">
something should be here, i do not want to be clicked
</div>

how to disable the abilities of left-clicking whithin that div?

Javascript Solutions


Solution 1 - Javascript

The CSS property that can be used is:

pointer-events:none

!IMPORTANT Keep in mind that this property is not supported by Opera Mini and IE 10 and below (inclusive). Another solution is needed for these browsers.

jQuery METHOD If you want to disable it via script and not CSS property, these can help you out: If you're using jQuery versions 1.4.3+:

$('selector').click(false);

If not:

$('selector').click(function(){return false;});

You can re-enable clicks with pointer-events: auto; ([Documentation][1])

Note that [pointer-events][1] overrides the [cursor][2] property, so if you want the cursor to be something other than the standard [![cursor][3]][3], your css should be place after pointer-events.

[1]: https://developer.mozilla.org/docs/Web/CSS/pointer-events "MDN Web Docs" [2]: https://www.w3schools.com/cssref/tryit.asp?filename=trycss_cursor [3]: https://i.stack.imgur.com/6VTW9.png

Solution 2 - Javascript

If you want it in pure CSS:

pointer-events:none;

Solution 3 - Javascript

Try this:

pointer-events:none

Adding above on the specified HTML element will prevents all click, state and cursor options.

http://jsfiddle.net/4hrpsrnp/

<div class="ads">
 <button id='noclick' onclick='clicked()'>Try</button>
</div>

Solution 4 - Javascript

You can use css

.ads{pointer-events:none}

or Using javascript prevent event

$("selector").click(function(event){
   event.preventDefault();
});

Solution 5 - Javascript

If using function onclick DIV and then want to disable click it again you can use this :

for (var i=0;i<document.getElementsByClassName('ads').length;i++){
    document.getElementsByClassName('ads')[i].onclick = false;
}

Example :
HTML

<div id='mybutton'>Click Me</div>

Javascript

document.getElementById('mybutton').onclick = function () {
    alert('You clicked');
    this.onclick = false;
}

Solution 6 - Javascript

How to disable clicking another div click until first one popup div close

Image of the example

 <p class="btn1">One</p>
 <div id="box1" class="popup">
 Test Popup Box One
 <span class="close">X</span>
 </div>

 <!-- Two -->
 <p class="btn2">Two</p>
 <div id="box2" class="popup">
 Test Popup Box Two
 <span class="close">X</span>
  </div>

<style>
.disabledbutton {
 pointer-events: none;
 }

.close {
cursor: pointer;
}

</style>
<script>
$(document).ready(function(){
//One
$(".btn1").click(function(){
  $("#box1").css('display','block');
  $(".btn2,.btn3").addClass("disabledbutton");
  
});
$(".close").click(function(){
  $("#box1").css('display','none');
  $(".btn2,.btn3").removeClass("disabledbutton");
});
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionReham FahmyView Question on Stackoverflow
Solution 1 - JavascriptAviadView Answer on Stackoverflow
Solution 2 - JavascriptAbe HendlishView Answer on Stackoverflow
Solution 3 - JavascriptRakesh_KumarView Answer on Stackoverflow
Solution 4 - JavascriptMOT ELView Answer on Stackoverflow
Solution 5 - JavascriptLY MOTView Answer on Stackoverflow
Solution 6 - JavascriptAshar ZafarView Answer on Stackoverflow