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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:20:27+00:00 2026-06-01T05:20:27+00:00

Check the following code: ​<div onclick=alert(‘Hi, from outer div!’);> <button onclick=alert(‘Hi, from button!’);>Tha button</button>,

  • 0

Check the following code:

​<div onclick="alert('Hi, from outer div!');">
    <button onclick="alert('Hi, from button!');">Tha button</button>, Click me!
</div>​​​​

Is there a way to prevent the outer div from firing an onclick when I click the button? Any idea how to cancel DOM level 0 events?​​

Note: I can’t use jQuery. It needs to work on Chrome, FireFox, IE6-9.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

  • 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-01T05:20:28+00:00Added an answer on June 1, 2026 at 5:20 am

    Yes. In most standard browsers, you call stopPropagation on the event object (live example | source):

    ​<div onclick="alert('Hi, from outer div!');">
        <button onclick="alert('Hi, from button!'); event.stopPropagation();">Tha button</button>, Click me!
    </div>​​​​
    

    In older copies of IE, you have to set the cancelBubble property to true instead:

    ​<div onclick="alert('Hi, from outer div!');">
        <button onclick="alert('Hi, from button!'); event.cancelBubble = false;">Tha button</button>, Click me!
    </div>​​​​
    

    …which means for broad compatibility you have to test which you’re dealing with, which gets ugly (live example | source):

    ​<div onclick="alert('Hi, from outer div!');">
        <button onclick="alert('Hi, from button!'); if (event.stopPropagation) { event.stopPropagation(); } else { event.cancelBubble = true; }">Tha button</button>, Click me!
    </div>​​​​
    

    These sorts of differences are why I always recommend moving away from the old DOM0-style handler and using a decent JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. These smooth over differences between browsers and provide a huge amount of utility functionality.

    For example, with jQuery, this HTML:

    ​<div id="theDiv">
        <button id="theButton">Tha button</button>, Click me!
    </div>​​​​
    

    …and this script (live example | source):

    $("#theDiv").click(function() {
        alert('Hi, from outer div!');
    });
    $("#theButton").click(function(event) {
        alert('Hi, from button!');
        event.stopPropagation(); // Even on IE, jQuery provides this
    });
    

    Or frequently with jQuery, you see people just doing return false; in their event handler. return false; in a handler, in jQuery, does two things: Stops propagation, and prevents any default action the event might have had (for instance, in a click handler on a link). stopPropgation doesn’t prevent the default.

    But this isn’t meant to be an advertisement for jQuery (though it is a very good library overall). Closure, YUI, Prototype, and all the others have similar functionality for letting you not worry about these sorts of browser incompatibilities.

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

Sidebar

Related Questions

I've got the following code: <div onclick=alert('div event'); style=cursor:pointer> some text <a href=asd.php id=link>click</a>
I have the following code $('#check-twitter').click( function() { if ($('#ShareTwitterPublish').val() == '0') { $('#ShareTwitterPublish').val('1');
I use the following code to check whether or not a certain div exists.
i've the following code, it's supposed to check if a variable from a DataList
I have the following code that will check to see if the div text
I have the following code: <div id=slideshow > <div class=check> <img src=images/slideshow/image01.jpg alt=Slideshow Image
To illustrate the question check the following code: class MyDescriptor(object): def __get__(self, obj, type=None):
Please check out my following code... public enum LogType { Debug, Info, Warn, Error,
I am using the following code to check if a variable is not nil
I am using following code to check whether a check box on my website

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.