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

  • Home
  • SEARCH
  • 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 870691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:31:08+00:00 2026-05-15T10:31:08+00:00

I would like to create a jQuery plugin with an API something like this:

  • 0

I would like to create a jQuery plugin with an API something like this:

$("#chart").pluginName().attr("my_attr");

Instead of these:

$("#chart").pluginName_attr("my_attr");
$.pluginName.attr("#chart", "my_attr");

Basically, instead of having to namespace every method that acts similar to ones in jQuery, I’d like to “scope” the methods to a custom api, where $("#chart).pluginName() would return an object such that get, attr, find, and a few others would be completely rewritten.

I’m sure this is not a well-liked idea as it breaks convention (does it?), but it’s easier and more readable, and probably more optimized, than the two options above. What are your thoughts?

  • 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-15T10:31:09+00:00Added an answer on May 15, 2026 at 10:31 am

    I’m experimenting with the idea.

    Seems like you could just modify the functions for the jQuery object that the plugin receives, and return that.

    Something like this:

    $.fn.tester = function() {  // The plugin
    
        this.css = function() {  // Modify the .css() method for this jQuery object
            console.log(this.selector);   // Now it just logs the selector
            return this;     // Return the modified object
        }
        return this;   // Return the modified object
    
    }
    

    http://jsfiddle.net/EzzQL/1/ (updated from original to overwrite .html() as well)

    $.fn.tester = function() {
        this.css = function() {  
            console.log(this.selector);  // This one logs the selector
            return this;
        }
        this.html = function() {
            alert(this.selector); // This one alerts the selector
            return this;
        }
        return this;
    };
    
    // Because .css() and .html() are called after .tester(),
    // they now adopt the new behavior, and still return a jQuery
    //    object with the rest of the methods in tact
    $('#test1').tester().css().html().animate({opacity:.3}); 
    
    
    // .css() and .html() still behave normally for this one
    //    that doesn't use the plugin
    $('#test2').css('backgroundColor','blue').html('new value');​
    

    EDIT:

    Alternatively, if you’re going to cache the elements to which the custom methods should be applied, you could .apply() the methods before you use them.

    To build on the example above:

    var $test1 = $('#test1');  // Cache the elements
    
    $.fn.tester.apply($test1,[this]);  // apply() the new methods
    
    $test1.css().html().animate({opacity:.3});  // Use the new methods
    

    ​

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

Sidebar

Ask A Question

Stats

  • Questions 524k
  • Answers 524k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's an instance of Connection class. Recent versions of SQLAlchemy… May 16, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer A lot of older documentation and articles say that CLOB… May 16, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer unfortunately I did not get any answer, I solved it… May 16, 2026 at 9:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would like to create a gallery slideshow using Jquery Tools Scrollable plugin. But
I would like to create something like this using ruby on rails. Im not
I have a jQuery tip function. I would like to create a button to
I would like to create a dropdown list where the last option will be
I'm developing a simple web quiz and using javascript, I would like to create
I would like to create a similar effect to Apple's Safari 4 Beta Top
I'm writing a jQuery plugin that stores some data in some cases. I'd like
I would like to create a fully functional page within my WordPress application (which
I would like to create a button that records date and time in the
I am trying to create a jQuery script that would remove a div/s based

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.