Search code examples
javascriptjquerycsshtml-listsstoppropagation

how to exclude an anchor from "e.stopPropagation()"


$pageslide.click(function(e) {
e.stopPropagation();
});

That prevents the #pageslide ID or Class from sliding if clicked, how do i exclude certain elements from being prevented, in this case i want to exclude <a></a> or <li></li>. Thanks in advance .


Solution

  • .target property of the event object refers to the target of the event, you can read this property:

    $pageslide.click(function(e) {
       if ( $.inArray(e.target.localName, ['li', 'a']) < 0 ) {
          e.stopPropagation();
       }
    });
    

    Or use .closest() method:

    if ( $(e.target).closest('a').length === 0 ) {
       e.stopPropagation();
    }