-2

Click on "child" also make the alert work. But what's the simple way to make click don't work on child elements?

Here is an example: https://fiddle.jshell.net/k8t5dpg2/

Html

<div id="secondary"> 1
<a>child</a>
</div>

Jquery

$('#secondary').click(function () {       
    alert('something')
                        });
j08691
  • 204,283
  • 31
  • 260
  • 272
R. S
  • 19
  • 1
  • 5
  • any child? do not add the `click` binding to.. (I guess I do not understand the question, can you elaborate?) – blurfus Mar 24 '17 at 15:16
  • pass the event object to your function and test its `target` - https://fiddle.jshell.net/k8t5dpg2/3/ – billyonecan Mar 24 '17 at 15:17

3 Answers3

1

Add an id to the child and :

$("#child").click(function () {       
    return false;
});
Sandwell
  • 1,451
  • 2
  • 16
  • 31
  • this is a bad approach for a number of reasons. you're adding additional event handlers which you don't need, you're returning false which is preventing default and stopping the event from propagating – billyonecan Mar 24 '17 at 15:22
0

Use event.target to determine what is being clicked.

Example:

$('#secondary').click(function (event) {
    var target = $(event.target);
    if (target.is(this)) {
       alert('something');
    }
});
0

You can try this.

$('#secondary').click(function (event) {
      if(event.target.nodeName ==='A') {
            return false;
      }else{
        alert('something');

       }
 });
Gurkan Yesilyurt
  • 2,635
  • 2
  • 20
  • 21