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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:31:02+00:00 2026-05-26T18:31:02+00:00

Let’s say I have a <ul> list: <ul class=products> … </ul> I want to

  • 0

Let’s say I have a <ul> list:

<ul class="products">
    ...
</ul>

I want to select it with jQuery, then add some functions to that object. For example, I’d like to add an addProduct(productData) function and a deleteProduct(productId) function. However, I’d like the functions to only be added to the object that’s returned by the selector. So for example, something like this:

var productList = $.extend($('ul.products'), {
    addProduct: function(productData) {
        // add a new li item
    },
    deleteProduct: function(productId) {
        // delete li with id
    }
});

How would I do this using jQuery? The key point here is that I only want to add the functions to an instance returned by a jQuery selector. In other words, I don’t want to modify jQuery’s prototype or create a plugin, since those will make the functions available across everything, whereas I only want to add the functions to one specific instance.

  • 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-26T18:31:02+00:00Added an answer on May 26, 2026 at 6:31 pm

    If you only want the addProduct and deleteProduct methods to feature on that single jQuery object, then what you’ve got will work fine; but you’ll have to keep a reference to that jQuery object/ only use it once, to preserve the existance of the addProduct and deleteProduct methods.

    However, these addProduct and deleteProduct methods are unique to that particular jQuery object; the methods won’t exist on any other jQuery objects you create;

    var productList = $.extend($('ul.products'), {
        addProduct: function(productData) {
            // add a new li item
        },
        deleteProduct: function(productId) {
            // delete li with id
        }
    });
    
    // Using this particular jQuery object (productList) will work fine.
    productList.addProduct();
    productList.removeProduct();
    
    // However, addProduct() does not exist on new jQuery objects, even if they use
    // the same selector.
    $('ul.products').addProduct(); // error; [object Object] has no method 'addProduct'
    

    The best way do to this would be to go-back-to-basics and define separate addProduct and deleteProduct functions, which accept a jQuery object. If you wanted to restrict these functions to they only worked on the ul.products selector, you could do;

    function addProduct(obj) {
        obj = obj.filter('ul.products');
    
        // do something with `obj` (its a jQuery object)
    }
    

    This approach would be recommended as it keeps the jQuery.fn API consistent; otherwise you’d be adding addProduct and removeProduct to some jQuery.fn instances but not others, or making their usage redundant in others. With this approach however addProduct and removeProduct are always there, but don’t get in anyones way if they don’t want to use them.


    Historical Notes

    This answer was originally written in November 2011, when jQuery 1.7 was released. Since then the API has changed considerably. The answer above is relevant to the current 2.0.0 version of jQuery.

    Prior to 1.9, a little used method called jQuery.sub used to exist, which is related to what you’re trying to do (but won’t help you unless you change your approach). This creates a new jQuery constructor, so you could do;

    var newjQuery = jQuery.sub();
    newjQuery.fn.addProduct = function () {
        // blah blah
    };
    newjQuery.fn.deleteProduct = function () {
        // blah blah
    };
    
    var foo = newjQuery('ul.products');
    foo.deleteProduct();
    foo.addProduct();
    
    var bar = jQuery('ul.products');
    bar.deleteProduct(); // error
    bar.addProduct(); // error
    

    Be careful though, the $ method alias would reference the old jQuery object, rather than the newjQuery instance.

    jQuery.sub was removed from jQuery in 1.9. It is now available as a plugin.

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

Sidebar

Related Questions

Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have some text as follows: do this, do that, then this,
Let's say I have a Person class with FirstName and LastName . I want
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say I have thousands of users and I want to make the passwords
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say for a moment that I have the following module in python: class
Let's say I have a 1 GB text file and I want to read
Let's say I have one class User, and it has a property of type

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.