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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:53:30+00:00 2026-05-23T22:53:30+00:00

Okay, I’m confused. I’ve got a global click event handler on document . On

  • 0

Okay, I’m confused. I’ve got a global click event handler on document. On the page I have a few links. Each link is handled by the same click event handler that, among other things, prevents the event from bubbling to the document level and prevents the link from executing. Of these links, one has a specific click handler that is supposed to do its thing and then pass off the event to the generic click event for the links. But it’s not.

document.onclick = function()
  {
   document.body.innerHTML += "You clicked me!"; 
};

  document.getElementsByTagName("a")[0].onclick = function(e) {
   this.innerHTML += " Click it!";
    e.stopPropagation();
    //This return false appears only to
    //prevent the link from its default action
  return false; 
  };
document.getElementById("link").onclick = function(e) {
  this.innerHTML += " Go ahead, ";
  //But this return false appears to stop
  //the propagation up to the previous event
  //I would think that removing the link below
  //would cause the event to propagate to the event
  //above which would then stop the propagation and
  //prevent the default, but apparently this is 
  //not the case; removing the line below causes
  //the link to load Google like normal
  return false;
};

How do I get the lower event to fire and up to the upper event which then cancels the event?

See what I mean here

  • 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-23T22:53:31+00:00Added an answer on May 23, 2026 at 10:53 pm

    Ha, duh. using element.on<event> is simply setting the property in the DOM for the element, which means each event can only have one handler. Instead, I need to use addEventListener combined with a proper use of event.preventDefault() and event.stopPropagation()

    In my first attempts, I was putting what I wanted to be the first handler second, but that really meant it was overriding the first. In this case, I need to put the handler I want first first, since the handler is being attached to the event.

    My revised code should be:

    document.onclick = function()
      {
       document.body.innerHTML += "You clicked me!"; 
    };
    document.getElementById("link").addEventListener("click",function() {
      this.innerHTML += " Go ahead, ";
    });
      document.getElementsByTagName("a")[0].addEventListener("click",function(e) {
       this.innerHTML += " Click it!"; 
        e.stopPropagation();
        e.preventDefault();
       });
    

    http://jsbin.com/ecozep/8/edit

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

Sidebar

Related Questions

Okay, I have setup up a handler for a div click but for some
Okay, what I currently have is a TabBarController with five tabs. Each of these
Okay so here is my database: I have a page on my .net form
Okay i have this problem with every page i make. im not sure what
Okay, i have a simple button on my page (MyPage) which fades out the
Okay, so I'm running a small test webserver on my private network. I've got
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, none of the previous questions I have seen with this error seem to
Okay so my question is this. Say I have a simple C++ code: #include
Okay, I really know this has GOT to be the long way around doing

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.