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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:12:16+00:00 2026-06-08T07:12:16+00:00

Consider that you want to add an ajax functionality to many links on your

  • 0

Consider that you want to add an ajax functionality to many links on your page.

<a href='http://domain/purchase/car' class='purchase'>Car</a>
<a href='http://domain/purchase/bag' class='purchase'>Bag</a>
<a href='http://domain/purchase/laptop' class='purchase'>Laptop</a>

and

<a href='http://domain/sell/car' class='sell'>Car</a>
<a href='http://domain/sell/bag' class='sell'>Bag</a>
<a href='http://domain/sell/laptop' class='sell'>Laptop</a>

Now you might define some variables to refer to these groups of links in your JavaScript code:

var purchaseLinks = $('.purchase'),
    sellLinks= $('.sell');

Well, enough story ;).

You can look at the problem in this fiddle and this one.

According to jQuery’s add() documentation, the return value is a new jQuery object. And again, AMAIK inside a function handler that is attached to an event of a jQuery object, this referes to the DOM element.

Why by using add() method, this of the handler function refers to the document? I don’t understand it. I can’t match my knowledge to make a logical perception. In other words:

jQueryObject1.click(function(){
    // Here, $(this) is the jQuery object
});

jQueryObject2.click(function(){
    // Here again, $(this) is the jQuery object
});

jQueryObject1.add(jQueryObject2).click(function(){
    // Here $(this) refers to the Document, why?
    // I think jQueryObject1.add(jQueryObject2) should equal jQueryObject3
});

UPDATE:

Thanks for your answers. I again refer readers to Live is Deprecated page, so that everyone can improve.

  • 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-08T07:12:18+00:00Added an answer on June 8, 2026 at 7:12 am

    The problem in your fiddle is the use of .live(). The jQuery docs state:

    Chaining methods is not supported. For example, $("a").find(".offsite,
    .external").live( ... );
    is not valid and does not work as expected.

    Fortunately for everyone, the .live() method is deprecated, so you don’t have to worry about stuff like that anymore. You can use on instead and take advantage of event delegation.


    Update (following the comment by @Esailija)

    This is what actually happens. You call jQuery here:

    var purchaseLinks = $('.purchase');
    

    That results in a jQuery object, which has a selector property. You then call .add(), which in turn calls the internal pushStack method:

    return this.pushStack(isDisconnected(set[0]) || isDisconnected(all[0]) ? all : jQuery.unique(all));
    

    The pushStack method creates the new jQuery object. It gets called with only one argument. Here’s the relevant parts of the method (note that name and selector are both undefined in our case):

    function (elems, name, selector) {
        // ...
    
        ret.context = this.context;
    
        if (name === "find") {
            ret.selector = this.selector + (this.selector ? " " : "") + selector;
        } else if (name) {
            ret.selector = this.selector + "." + name + "(" + selector + ")";
        }
    
        // Return the newly-formed element set
        return ret;
    }
    

    Also note that this.context is now the document (since your original jQuery object didn’t specify a context, the highest possible context is assumed).

    So the newly formed element set doesn’t have a selector property, and has the document as its context. When we then call .live(), jQuery simply calls .on() like this:

    jQuery( this.context ).on( types, this.selector, data, fn );
    

    And there we can see the problem. The context is the document, and there is no selector, so the event handler gets bound to the document. The above line in our case is effectively this:

    jQuery( document ).on( "click", function() {
        //Your event handler
    });
    

    And you can see this by the fact that your alert is triggered by clicking anywhere in the document, not just on the links.

    The moral of the story? Stop using .live()!

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

Sidebar

Related Questions

Consider the following code: while(true) { someFunction(); Thread.sleep(1000); } What I want is that,
Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
I have a week calendar that holds events, and want that users can't add
I want to add a simple function to a central place so that I
I am using RDLC report in Win-form. I want to add the Below Page
Consider that I want to create 10 websites in IIS, each of them pointing
Consider the following. You have a class that you want to serialize with XmlSerializer
Consider that we have a DIV with fixed height. Without a defined width ,
Consider that I've a excel sheet in below format: person age Foo 29 Bar
Consider that there is a bunch of tables which link to countries or currencies

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.