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 7883995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:40:54+00:00 2026-06-03T04:40:54+00:00

When I use event.preventDefault() on a link it works, however when I use it

  • 0

When I use event.preventDefault() on a link it works, however when I use it on a button doesn’t!

DEMO

My code:

<a id="link" href="http://www.google.com">link</a>
<button id="button" onclick="alert('an alert')">button</button>​

$('#link').click(function(event){
  event.preventDefault();
});
$('#button').click(function(event){
  event.preventDefault();
});

​
Link action is cancelled, but when I click on the button, still executes the onClick action.

Any help? what I want to do is to prevent the button onClick action without changing the button html (I know how to do

$('#button').removeAttr('onclick');

  • 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-06-03T04:40:55+00:00Added an answer on June 3, 2026 at 4:40 am

    You want event.stopImmediatePropagation(); if there are multiple event handlers on an element and you want to prevent the others to execute. preventDefault() just blocks the default action (such as submitting a form or navigating to another URL) while stopImmediatePropagation() prevents the event from bubbling up the DOM tree and prevents any other event handlers on the same element from being executed.

    Here are some useful links explaining the various methods:

    • http://api.jquery.com/event.preventDefault/
    • http://api.jquery.com/event.stopPropagation/
    • http://api.jquery.com/event.stopImmediatePropagation/

    However, since it still doesn’t work it means that the onclick="" handler executes before the attached event handler. There’s nothing you can do since when your code runs the onclick code has already been executed.

    The easiest solution is completely removing that handler:

    $('#button').removeAttr('onclick');
    

    Even adding an event listener via plain javascript (addEventListener()) with useCapture=true doesn’t help – apparently inline events trigger even before the event starts descending the DOM tree.

    If you just do not want to remove the handler because you need it, simply convert it to a properly attached event:

    var onclickFunc = new Function($('#button').attr('onclick'));
    $('#button').click(function(event){
        if(confirm('prevent onclick event?')) {
            event.stopImmediatePropagation();   
        }
    }).click(onclickFunc).removeAttr('onclick');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://api.jquery.com/event.preventDefault/ If this method is called, the default action of the event will not
I use MouseMove event to move objects(say labels). simple principle(schematic): OnMouseMove(e MouseEventArgs) if e.Button
The following code is a simplified version of what I use for event dispatching.
i'm making a bookmarklet to my website, and for now i have: <a href=javascript:location.href='http://website.com/compartir_link.php?url='+encodeURIComponent(location.href)
I would like to use the event's preventDefault method inside jsonp's callback. I must
Here is my function: $(.link).click(function(event){ event.preventDefault(); event.stopPropagation(); pageLoad = $(this).data('page'); $('div#loading').fadeIn(); $('div#content').load('pages/'+pageLoad); }); I
I use the plugin waypoints ( http://imakewebthings.com/jquery-waypoints/ ) * EDIT : working solution :
Why we need to use event Propagation?
I want to use Event Aggregator to allow communication between presenters on the page.
I've studied some CQRS sample implementations (Java / .Net) which use event sourcing as

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.