Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neglect a function in JavaScript? [duplicate]

When I click mainDiv, the function mainDiv() will be invoked, when I click subDiv, both mainDiv() and subDiv() functions are invoked.

I want to invoke only the subDiv() function when I click subDiv. How can I achieve this?

CODE:

<div onclick="mainDiv()">
  show main div
  <div onclick="subDiv()">
    show sub div
  </div>
</div>
<script type="text/javascript">
  function  mainDiv(){
    alert("main div is clicked");
  }
  function  subDiv(){
    alert("sub div is clicked");
  }
</script>
like image 275
shihabudheen Avatar asked Mar 07 '13 12:03

shihabudheen


Video Answer


1 Answers

use e.stopPropogation()

HTML

<div onclick="subDiv(event)">  //<--- pass event parameter

javascrip

function  subDiv(e){    

   if(e.stopPropagation){  // check stoppropogation is avilable
      e.stopPropagation();  //use stopPropogation
   }else{
      e.cancelBubble = true;  // for ie8 and below
   }
   alert("sub div is clicked");
}
like image 132
bipen Avatar answered Sep 29 '22 00:09

bipen