Fire event when clicking on div except link

I want to fire an event when I click on a div, but I do not want this event to fire if I click on a specific link on that div:

my js

$("#myDiv").not("#myLink").click(function () {
    alert("clicked");
});

//Or

$("#myDiv:not(#myLink)".click(function () {
    alert("clicked");
});

$("body").on('click', '#myLink', function(e) {
    e.stopPropagation();
});

my html

<div id="myDiv">
    <a href="#" id="myLink">Some link</a>
</div>

Here is jsfiddle

EDIT: Actually my real code is with the On method , I already tried stopPropagation, but in this case it doesn't work

+5
source share
3 answers

Add to js:

$("#myLink").click(function(e) {
    e.stopPropagation();
});

Updated your violin

+6
source

In order not to repeat the code that stops execution for each link placed in the div, you can use stopPropagation .

$('#myDiv a').click(function(e) {
    e.stopPropagation(); //stops default link action
    //do your stuff
});

, (<a>) , , #myDiv div.

+4

$("#myDiv").click(function (ev) {
     alert("clicked");


}).children().click(function(e) {
  return false;
});

http://jsfiddle.net/SjDdN/7/

0
source

All Articles