Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: stopPropagation for link

How can i stop propagation for link?

<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
    $(function() {
        $("#g").click(function(event){
            alert("Link clicked");
            event.stopPropagation();
        });
    });
</script>
<a id="g" href="http://google.com">Google</a>

I want the browser don't go to google, just show alert.

like image 394
Dmitry Avatar asked Jul 04 '12 17:07

Dmitry


2 Answers

You have to use event.preventDefault(); to prevent the default action -navigating to Google- from happening.

like image 176
Rob W Avatar answered Oct 16 '22 10:10

Rob W


You will need event.preventDefault() and also return false

like image 30
James Avatar answered Oct 16 '22 08:10

James