Search code examples
jqueryclickstoppropagation

StopPropogation - not working to disable parent anchor tags


I am currently trying to change the color of a child element on click. The parent element links to another page. I was hoping stopPropogation would work, but it isn't. The following is my jQuery code:

$('.child').click(function(){

    if($(this).hasClass('pinned')){

        $(this).removeClass('pinned')

    }

    else{

        $(this).addClass('pinned')

    }

}); 

    // disable parent link when pinning

    $('.child').click(function(event){

        event.stopPropagation();

    }); 

If it matters, the anchor tag surrounds the grandparent.

Where am I going wrong?


Solution

  • $('.child').click(function(e) {
        if($(this).hasClass('active')){
            $(this).removeClass('pinned');
        }
        else {
            $(this).addClass('pinned');
        }
        e.stopPropagation();
        return false;
    }); 
    

    You can use e.stopPropagation in same function.