Stop nested function call

Is there any possibility to stop a function call.

If I click on div which have class ‘one’, myFun() will be trigger, but display() which is within the myFun() it should not be run.

If I click on div which have class ‘two’, myFun() will be trigger including display().

<div class="one">
        <div class="two">click</div>
</div>

function myFun(){ 
//other tasks are going here
 display();
  }

$('.one').on('click', function(){
   myFun();
}

$('.two').on('click', function(){
myFun();
}

Events bubble up the top of the tree, you can stop this within an event handler with event.stopPropagation()

$('.two').on('click', function(event){
  myFun();
  event.stopPropagation();
}

does it stop the inner function call, hope I am clearing it while clicking on div with class ‘one’ myFun should trigger within that function need to stop the display() function call.

The best answer can be something close to this.


    function myFun (event) {  
      if (event.data) {  
        display();  
      }
      
      return event.stopPropagation();
    }


    $('.one').on('click', true, myFun);

    $('.two').on('click', myFun);

1 Like

Sorry I don’t understand what you are asking, if the event in .two calls event.stopPropagation() it prevents the event from “bubbling up” to the parent .one and triggering that event.

myFun() should call eihter click of any class(.one, .two), but if I click on the ‘.one’ class need to call myFun() but it shouldn’t call display() function which is within that myFun().

This sounds really convoluted but you can pass arguments through to myFun so you know which is which. You’ll still need to stop propagation or both events will fire when you click on .two

<div class="one">
  <div class="two">click</div>
</div>

function myFun(one){ 
  // other tasks are going here
  if (one) {
    display();
  }
}

$('.one').on('click', function(){
  myFun(true);
}

$('.two').on('click', function(event){
  event.stopPropagation();
  myFun(false);
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.