Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4333022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:20:10+00:00 2026-05-21T10:20:10+00:00

Before I start writing huge swathes of code that don’t work I thought I’d

  • 0

Before I start writing huge swathes of code that don’t work I thought I’d ask this question.

event.preventDefault() only cancels the default action of the click event doesn’t it?

Theoretically I should be able to bind mutiple click event handlers in jQuery to a given target to perform different actions like Ajax posts and Google tracking.

Am I wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:20:11+00:00Added an answer on May 21, 2026 at 10:20 am

    event.preventDefault() only cancels the default action of the click event doesn’t it?

    It cancels the browser’s default action of the event (not just the click event) (W3C docs, jQuery docs). So for instance, in the form submit event, it prevents the form being submitted by the browser. It doesn’t stop anything you’re doing in code, and it doesn’t stop bubbling; that’s what stopPropagation is for (W3C docs, jQuery docs).

    So say you have a link in a div, and you have the click event hooked on both the link and the div. If the link’s event handler calls preventDefault, the browser won’t do its default action (following the link), but the event continues to bubble up the DOM to the link’s parent element, the div, and so you’ll see the event on your click handler there, too. Any actions you’re taking in code in either handler will be unaffected by your calling preventDefault.

    In your comment below, you ask about multiple handlers on the same element. Neither preventDefault nor stopPropagation affects those, they’ll still get fired…unless you use stopImmediatePropagation, which tells jQuery to stop the event dead in its tracks (but doesn’t prevent the browser’s default action).

    I should probably round this out by saying that if you return false from your event handler, that tells jQuery to prevent the default and stop bubbling. It’s just like calling preventDefault and stopPropagation. It’s a handy shortcut form for when your event handler is taking full control of the event.

    So, given this HTML:

    <div id='foo'><a href='http://stackoverflow.com'>Q&amp;A</a></div>
    

    Example 1:

    // Here we're preventing the default but not stopping bubbling,
    // and so the browser won't follow the link, but the div will
    // see the event and the alert will fire.
    $("#foo").click(function() {
        alert("foo clicked");
    });
    $("#foo a").click(function(event) {
        event.preventDefault();
    });
    

    Example 2:

    // Here we're stopping propagation and not preventing the default;
    // the browser will follow the link and the div will not be given
    // a chance to process the event (no alert, and more to the point,
    // code in the div's handler can't prevent the default)
    $("#foo").click(function() {
        alert("foo clicked");
    });
    $("#foo a").click(function(event) {
        event.stopPropagation();
    });
    

    Example 3 (you’ll only rarely see this):

    // Here we're doing both, and so the browser doesn't follow the
    // link and the div doesn't see the event (no alert).
    $("#foo").click(function() {
        alert("foo clicked");
    });
    $("#foo a").click(function(event) {
        event.preventDefault();
        event.stopPropagation();
    });
    

    Example 4:

    // Shorter version of Example 3, exactly the same effect
    $("#foo").click(function() {
        alert("foo clicked");
    });
    $("#foo a").click(function() {
        return false;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

before I start I want to point out that I tagged this question as
I'm trying to get some better logic behind this before I start writing code.
Before I start, I want to make it clear that my code is working
Lots of people talk about writing tests for their code before they start writing
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
(Before I start, yes I have asked a similar question before; unfortunately due to
I am asking my self many times before start writing a new app or
Before I start writing my own custom classes, I was wondering if there was
well. i was use Reactive Extensions (Rx) package async Request,Before you can start writing
When i start writing something complex, I find that restart the writing like 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.