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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:33:42+00:00 2026-06-09T03:33:42+00:00

I have a DOM structure which looks something like below – <div id=’MyDiv’> <span>Hello

  • 0

I have a DOM structure which looks something like below –

<div id='MyDiv'>
    <span>Hello MyDiv</span>
    <p class='MyClass'>
        <img src='image.png' alt='myImage' width='300px' height='300px' />
        <a href='#goToNextDiv'>Go to next</a>
    </p>
</div>

Now MyDiv has a click event handler attached with prototype library, the source of which isn’t accessible to me. I want to remove this handler from the div and insert another one. I’ve tried the following code for this –

var content = document.getElementById('MyDiv');
content.addEventListener('click', function(event){
    event.preventDefault();
    event.stopPropagation();

    alert('This is my handler');    

    return false;
}, true);

The problem is, this code also prevents the click event of the anchor tag. Clicking on the div causes my event handler to execute, which is desired, but it also stops the anchor tag to execute it’s normal behavior (which is jumping to the div goToNextDiv).

Now I have tried commenting out the event.stopPropagation() line, which again enables the event propagation and allows the anchor to behave accordingly, but then it also executes the previous click event handler of MyDiv.

How can I remove the previous handler from the div and attach my new one so that –

  1. the previous event handler for the div doesn’t get called
  2. my div event handler executes
  3. the anchor tag also works (there could be many anchor tags inside
    the div, in any place)
  • 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-09T03:33:45+00:00Added an answer on June 9, 2026 at 3:33 am

    You can check if the click target was an anchor and not prevent the default action.

    var content = document.getElementById('MyDiv');
    content.addEventListener('click', function(event){
        if (event.target.tagName != 'A'){//or some other way to tell if it is a link
            event.preventDefault();
            event.stopPropagation();
    
            alert('This is my handler');    
    
            return false;
        }
    }, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DOM structure that looks like this: <div id=comment-1234 class=comment>...</div> <div id=comment-2391
I have a DOM structure that looks like this: <div class=chapter> <section></section> <section></section> </div>
I have the following DOM structure: /*:DOC += <div id='testDiv' class='testDivClass'><div id='innerTestDiv'></div></div><span id='testSpan' class='testSpanClass'><span
I have the following DOM structure of interest: <div id=portfolioItems> <div class=portfolioItemsLine> <div class=portfolioItem
I have the following DOM structure: <div class=city> <div class=a>Delete</div> <div class=b>Selected</div> <div class=c>Delete</div>
I have the following dom structure: <div class=ui-tabs> <ul> <li class=ui-state-default ui-tabs-selected ui-state-active> <a
So I have a DOM document that looks essentially like this <categories> <category id=1/>
I have a simple DOM code like the following <div> <div> </div> </div> I
I have the the following DOM structure which I want to sort according to
I have a DOM structure sometihng like this (I don't want to show other

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.