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

  • Home
  • SEARCH
  • 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 6003291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:06:03+00:00 2026-05-23T01:06:03+00:00

I know that JavaScript return false means prevent default event (like preventDefault() method). #1

  • 0

I know that JavaScript return false means prevent default event (like preventDefault() method).

#1

<a href="http://stackoverflow.com" onclick="return false;">click</a>

#2

<a id="a" href="http://stackoverflow.com">click</a>
<script>
document.getElementById('a').addEventListener('click', function(){  return false; }, false);
</script>

I wonder why just #1 prevent default event but not #2. Did I make some mistake?

Edit:
Sorry, I missed an id of anchor tag and third argument of code in #2. I added it but it’s still not working.

  • 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-23T01:06:04+00:00Added an answer on May 23, 2026 at 1:06 am

    There are two problems with your second example:

    1. You’re using document.getElementById but you’re giving a tagname. You can probably use document.getElementsByTagName (which returns a NodeList you would then index into), or give the element an id attribute and use getElementById to look it up.

    2. Your addEventListener call is missing the third argument, which is required. So:

      document.getElementById('someId').addEventListener(
          'click',
          function(){  return false; },
          false // <=== Third argument, you almost certainly want `false`
      );
      

    Regarding your question about return false: If you’re using a browser that supports addEventListener and you’re hooking up event handlers with it, no, you don’t use return false to prevent the default action. Instead, you use event#preventDefault. (You can also use event#stopPropagation to stop the event bubbling, but DOM0’s return false doesn’t do that, so that’s just an extra bonus.)

    document.getElementById('someId').addEventListener(
        'click',
        function(e){
            // Prevent the default action of the link
            e.preventDefault();
    
            // Stop the event propagating (bubbling) to the parent element
            e.stopPropagation();
        },
        false
    );
    

    Also note that there are a lot of people using IE8 and earlier, which do not support addEventListener (instead, they support Microsoft’s original attachEvent; but not all versions support preventDefault or stopPropagation).


    Somewhat off-topic: As you can see, handling events in a cross-browser way is a hassle. It’s one of the many hassles you can avoid by using a decent JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. They smooth over browser differences and provide lots of helpful utility functionality, so you can focus on what you’re actually trying to build (rather than worrying about how IE7 and earlier have a broken version of getElementById).

    Examples:

    jQuery: jQuery provides a return false feature (although it’s different from the DOM0 one you’re talking about), and it also ensures that event#preventDefault and event#stopPropagation work regardless of how the underlying browser handles events. So either of these work with jQuery:

    // Using return false (which prevents the default AND -- unlike DOM0 stuff -- stops propagation)
    $('#someId').click(function() { return false; });
    
    // Using the DOM standard event methods -- even on browsers that don't support them
    $('#someId').click(function(e) {
        e.preventDefault();
        e.stopPropagation();
    });
    

    Prototype: Prototype provides the DOM standard event methods (even on browsers that don’t support them) as well as event#stop which is just a combination of preventing the default and stopping propagation:

    // Using `stop` (which prevents the default and stops propagation)
    $('someId').observe('click', function(e) { e.stop(); });
    
    // Using the DOM standard event methods -- even on browsers that don't support them
    $('someId').observe('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
    });
    

    Other libraries will offer similar functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that in JavaScript, creating a for loop like this: for(int i =
In JavaScript, I know that a closure is can be defined as a nested
I need to have some information about the scoping in JavaScript. I know that
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
Does anybody know how to execute javascript functions in an html that is loaded
I know that you can insert multiple rows at once, is there a way
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on

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.