Dmitry
Dmitry

Reputation: 7563

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.

Upvotes: 8

Views: 7059

Answers (3)

andres descalzo
andres descalzo

Reputation: 14967

if you just want to not go to google, just return false.

$("#g").bind('click', function(event){
   alert("Link clicked");
   return false;
});

Upvotes: 2

Nhu Trinh
Nhu Trinh

Reputation: 13956

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

Upvotes: 4

Rob W
Rob W

Reputation: 349042

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

Upvotes: 19

Related Questions