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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:16:18+00:00 2026-05-21T09:16:18+00:00

I am receiving unexpected behavior with a jQuery event handler. It is very similar

  • 0

I am receiving unexpected behavior with a jQuery event handler. It is very similar to the problem discussed in this question: Why does my jQuery event handler fail when attached to multiple elements? .

Basically, a simplified example is, my page calls

   $(document).ready(function(){
        $('a.submit').click(function(e){
              e.preventDefault();
              alert(this.href);
        });
   });

Assume the markup is:

<html>
    <body>
        <a href="url1" class="submit">text1</a>
        <a href="url2" class="submit">text2</a>
        <a href="url3" class="submit">text3</a>
    </body>
</html>

All of the links with the submit class except for the last one raise the event. The last link with .submit on the page does not get wired to the event handler. Replacing click(fn) with live('click',fn) works.

My problem is I don’t understand why the original handler fails. All the links are rendered at the same time, and I am wrapping my handler-subscription in a $(document).ready(), which should ensure the DOM is loaded.

I need to figure this out, because I have a more complex situation where the handlers are registered deep inside a minified javascript file, so it would be very helpful if I could understand how to prevent this bug instead of fixing it when it occurs.

One more thing to note is that I am receiving the same weird behavior in Firefox, Chrome and IE. I am using jQuery1.5.1

  • 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-21T09:16:19+00:00Added an answer on May 21, 2026 at 9:16 am

    Considering the html you provided:

    <a href="url1" class="submit">text1</a>
    <a href="url2" class="submit">text2</a>
    <a href="url3" class="submit">text3</a>
    

    and JavaScript:

    $(document).ready(function(){
            $('a.submit').click(function(e){
                  e.preventDefault();
                  alert(this.href);
            });
       });
    

    it must work. no doubt about it. However, if your anchor tags <a> are being created dynamically, you can’t bind the direct click event to those but through live such as

    $('a.submit').on('click', function(e){
        //magic here
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Receiving a segfault when running this very short script in Ubuntu. from osgeo import
I am currently receiving this error: Validation of viewstate MAC failed. If this application
I'm attempting to get the height of an element using jQuery, but am receiving
I'm having trouble overriding a ModelForm save method. This is the error I'm receiving:
I have been receiving errors when trying to save and run this Python 3.1
I'm receiving from the socket a MAC address in this format: 0024e865a023 (hex converted
I am receiving this error: The ViewData item that has the key 'DepartmentId' is
Receiving this error: a different object with the same identifier value was already associated
Im receiving an XML RSS feed. One of the tags look like this: <georss:point>55.0794503724671
Im receiving this error in my Rails app and cant find the solution. I

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.