Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent concurrent javascript executions?

i have html code block like this

<div id="some-div">
    <a href="#" class="link-click">some link text</a>
    <div id="small-text">here is the small text</div>
    //another text block 
</div>

<script type="text/javascript">
  $('#some-div').click(function(){//some task goes here});
  $('.link-click').click(function(){//some task goes here for link});
  $('#small-text').click(function(){//some task goes here for small div});
</script>

when we click on the div(id=some-div) popup message coming but when i click on the link(class=link-click) i need to run another javascript function. but the thing is when i click on link-click it also runs some-div function too. means on click both function gets run.

how do i prevent concurrent javascript function execution. i'm using jquery too. thanks

like image 856
Gayan Avatar asked Jun 22 '11 07:06

Gayan


2 Answers

You could use event.stopPropagation() to avoid having the event bubbling up the DOM tree.

like image 91
kgiannakakis Avatar answered Nov 01 '22 02:11

kgiannakakis


$('.link-click').click(function(e){
  e.stopPropagation();
});
like image 22
Jose Faeti Avatar answered Nov 01 '22 02:11

Jose Faeti