Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply click method to outer div but not inner div [duplicate]

In this example I want the alert to be triggered when the black outer div is clicked but not when anything inside the inner div is clicked.

Currently when I click the inner button it triggers the alert.

$("#outer").click(function() {
  alert("triggered");
});
#outer{
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
}
#inner{
  background-color: #fff;
width: 300px;
height: 300px;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="outer">

  <div id="inner">
    <button>inner button</button>
  </div>
</div>

I want inside the inner div nothing to trigger the alert. How do i do this?

(for some reason inner button isn't showing in snippet, here is codepen link: https://codepen.io/GuerrillaCoder/pen/Pmvmqx)

like image 350
Guerrilla Avatar asked May 28 '17 08:05

Guerrilla


7 Answers

You just need to check for id of element, as inner is child element, it will always propagate the event to parent element (outer).

$("#outer").click(function(event) {
 if(event.target.id=="outer"){
  alert("triggered");
 }
});
like image 57
Jayakrishnan Avatar answered Oct 05 '22 18:10

Jayakrishnan


You can use event.stopPropagation(); when clicking the inner div:

$("#outer").click(function() {
    alert("triggered");
});
$('#inner').click(function (evt) {
    evt.stopPropagation();
});
#outer{
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: #000;
    display: flex;
    justify-content: center;
    align-items: center;
}
#inner{
    background-color: #fff;
    width: 300px;
    height: 300px;
    text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="outer">
   <div id="inner">
       <button>inner button</button>
   </div>
</div>
like image 32
Koby Douek Avatar answered Oct 05 '22 18:10

Koby Douek


In the click function check event target equal to outer

    $("#outer").click(function(e) {
        if (e.target.id === "outer"){
            alert("triggered");
         }
    });
like image 37
Adharsh M Avatar answered Oct 05 '22 18:10

Adharsh M


I think event.stopPropagation() method would be most useful solution.you only need to call it on the children of outer div:

$("#outer").on('click', function () { 
   alert("triggered");
}).children().on('click', function (e) {
    e.stopPropagation();
});

Also check this Solution.

like image 41
Oghli Avatar answered Oct 05 '22 18:10

Oghli


Pure javascript solution, if anyone cares -))

var inner = document.getElementById("inner");
var btn = inner.querySelector("button");
document.getElementById("outer").addEventListener("click", function(e) {
  (e.target == btn) || (e.target == inner) ? console.log("click triggered from"+ e.target+". so we do not alert anything") : alert("something"); 
});
#outer{
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
}
#inner{
  background-color: #fff;
width: 300px;
height: 300px;
  text-align: center;
}
<div id="outer">

  <div id="inner">
    <button>inner button</button>
  </div>
</div>
like image 36
marmeladze Avatar answered Oct 05 '22 18:10

marmeladze


Another way to do this:

$("#outer").click(function() {
  alert("triggered");
});
$("#inner").click(function(e) {
 e.stopPropagation() 
});
#outer{
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #000;
  display: flex;
  justify-content: center;
  align-items: center;
}
#inner{
  background-color: #fff;
width: 300px;
height: 300px;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="outer">
  <div id="inner">
    <button>inner button</button>
  </div>
</div>
like image 38
Bhuwan Avatar answered Oct 05 '22 18:10

Bhuwan


You can use event.target to check event if is triggered by parent or by its children:

$("#outer").click(function(e) {
  if( e.target !== this ) {
       return;
   } else {
       alert("triggered");

   }
});
like image 36
Vibhanshu Avatar answered Oct 05 '22 16:10

Vibhanshu