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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:33:32+00:00 2026-06-01T09:33:32+00:00

This is a deceptively tricky little bit of HTML/JS that has either me or

  • 0

This is a deceptively tricky little bit of HTML/JS that has either me or jQuery thrown for a loop (my money’s on me).

It’s demoed in this fiddle.


…but here’s the scenario. An anchor is wrapped with a span:

<span> the span
    <a onclick="alert('onclick HTML attritbute fired!')" href="http://www.href-attribute-    fired.com">the link</a>
    wraps the link
</span>

The span has a click handler that clicks the anchor. This needs to be .one() to avoid looping forever because of event bubbling (I also tried .on() and .off() to short circuit the loop with the same result – see fiddle).

$('a').click(function() {
    alert("jQuery click fired!");
});

$('span').one("click", function() {
    alert('(span clicked, let us see what other events fire...)');
    $(this).find('a').click();
});

What you’ll notice when you click on the link itself is that:

  1. onclick HTML attribute fired!
  2. jQuery click fired!
  3. (span clicked, let us see what other events fire…)
  4. onclick HTML attribute fired!
  5. jQuery click fired!
  6. the href is followed

It loops through the JS events an extra time because of the event bubbling up to the span, which is a little goofy and inelegant but totally expected and makes sense to me. The key part here is that after all the JS events are through, the href is followed.

When you click on the outer span, this is the order of events:

  1. (span clicked, let us see what other events fire…)
  2. jQuery click fired!
  3. onclick HTML attribute fired!
  4. … {crickets!?!} ….

Even though the anchor’s JS click events have fired, the href is never followed.


I’m guessing that .one() doesn’t unbind the click event for the span until the second time through, after bubbling up from the anchor, and at at that point, the event propagation completely dies. Since the href is not followed until all click events are finished, when the signal dies at the span the anchor doesn’t know to fire the go-to-href event.

…but is that really what happens? What gives?

What is a better way to approach this problem (I’d like to avoid JS pulling off the href and setting window.location manually for a variety of reasons)?

  • 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-01T09:33:33+00:00Added an answer on June 1, 2026 at 9:33 am

    The link is followed in the first instance (when you click it directly) because that’s the way <a> tags work, and none of your handlers asks that the default behavior be cancelled.

    Triggering a “click” via jQuery on an <a> tag explicitly does not follow the link; that’s just how jQuery works.

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

Sidebar

Related Questions

This must be a very simple solution that has eluded me this last hour.
This issue is so deceptively simple that I'm having trouble getting my head around
This seems deceptively simple. How do you override the onclick event in the following
This is my first time creating a PHP form that will run a MySQL
This Apple tech note: http://developer.apple.com/library/ios/#qa/qa2010/qa1699.html suggests storing internal user documents in a subdirectory off
This is a super simple issue that I can't figure out. I want to
This has been posted, but I've tried lot of solutions found on SO and
I have a variable in a package ( rec in this case) that needs
This is my first attempts at creating a data-mart/warehouse and I am a little
This link describes a function on a Money object for adding funds together of

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.