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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:29:14+00:00 2026-06-12T03:29:14+00:00

I read the jquery documentation of plugin authoring and am familiar with that. However,

  • 0

I read the jquery documentation of plugin authoring and am familiar with that. However, the examples given always operate on a set of previously matched elements. I want to create a function that can do both:

// example usage of my to-be-created plugin function

// this is the way described in the docs, and I know how to do that
$("a").myFunction ()

// but I also want to be able to call the function without a collection:
$.myFunction ();

If $.myFunction ()is called without a collection to operate on, it would create it’s own collection of what elements to match – kind of an initialization process (but not necessarily run only once). Also, $.myFunction ()should maintain chainability.

The pseudocode of what I want to achieve:

// [...]
function myFunction (): {
    if (notCalledOnACollection) {
        // this should run when called via $.myFunction ()
        $elements = $("a.myClass");
    }
    else {
        $elements = $(this);
    }
    return $elements.each (function () {
        // do sth. here 
    });
}

I would really like to keep all of the functions implementation/functionality within a single function definition, and not have two separately named functions or two equally named functions in two separately places within the jQuery object.

And of course I could add a parameter myFunction (do_init) that indicates what branch of the if statement to execute, but that would clutter my argument list (I want to use that approach for multiple plugins, and there will be argumentes to myFunction () that I just left out here for simplicity).

Any good suggestions?

  • 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-12T03:29:15+00:00Added an answer on June 12, 2026 at 3:29 am

    By simply adding another reference in the plugin definiton, you can easily use the standard plugin code:

    (function( $ ) {
        $.myPlugin = $.fn.myPlugin = function(myPluginArguments) {
            if(this.jquery)
                //"this" is a jquery collection, do jquery stuff with it
            } else {
                //"this" is not a jquery collection
            }
        };
    
        $.fn.myPlugin.otherFunc = function() {
        };
    })( jQuery );
    

    The only difference here is the $.myPlugin = part which allows you to directly call your plugin without running jquery’s selector function. Should you decide you need other functions or properties, you can create them as properties of your plugin.

    Usage:

    //using a selector (arguments optional)
    $(".someClass").myPlugin();
    
    //using the options variable - or whatever your arguments are
    $.myPlugin({id: "someId"});
    
    //accessing your other functions/properties
    $.myPlugin.otherFunc();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using 'Uploadify' JQuery plugin for file upload. I read the documentation and
I looked at JQuery documentation , read FAQ and saw a lot of examples.
I read that docs.jquery.com uses wiki to do the API documentation. I have been
I am using jQuery to read data in a HTML page. On that page,
I've read an article about 20 top jQuery tips that uses the below code
I've read that Microsoft now bundles jQuery with Visual Studio . How then do
To make my question more specific, I read the documentation on .each() for jQuery,
I am trying to setup an autocomplete field. I'v read the JQuery UI documentation,
Having read the documentation for the remove-function at http://api.jquery.com/remove/ , was under the impression
I've read the jQuery documentation on event handling, but I still can't really understand

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.