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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:58:29+00:00 2026-06-11T11:58:29+00:00

(function($) { $.fn.myFunction = function(config) { var defaults = { setting1: ‘myDiv’, setting2: ”

  • 0
(function($) {
    $.fn.myFunction = function(config) {

        var defaults = {
            setting1: 'myDiv',
            setting2: ''
        };
        var config = $.extend(defaults, config); 

        //code here


    };

})(jQuery)


$(document).ready(function() {

    $.fn.myFunction({
        setting1: 'myDiv',
        setting2: ''
    });

});

This is how I’ve been using jQuery plugins, but I recently learned it should be used like:

$('#myDiv').myFunction({
    setting1: 'myDiv',
    setting2: ''
}); 

1) I take it this allows the usage of $(this) for $('#myDiv')?

2) Is $(document).ready(function() required?

3) Is there anything detrimental about the way I have been using this jQuery function?

4) If $('#myDiv').myFunction() is the proper usage, how would you call the function if it is simply a function to run at document ready – see my usage here: https://stackoverflow.com/a/12316349/1455709. Is this simply an incorrect usage of the function?

  • 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-11T11:58:30+00:00Added an answer on June 11, 2026 at 11:58 am

    Calling $.fn.myFunction() and calling $('#myDiv').myFunction() are two different things. There is no right or wrong – it depends upon what you’re doing.

    Calling $.fn.myFunction()

    This is essentially a static function and you can use it like that if you want. In the jQuery world, .fn is like .prototype so $.fn.myFunction() is like calling jQuery.prototype.myFunction(). It is allowed, but it’s a static function call that is not associated with a specific jQuery object. If you just want a static function call, then you can do this, but this is not normally how the jQuery prototype is used and I would not generally recommend it. If you just want a static function and want to use the jQuery namespace, you can just do this:

    $.myFunction = function(args) {/* your code here */};
    

    and then call it like:

    $.myFunction();
    

    As there is no need to use the prototype at all.

    Calling $('#myDiv').myFunction()

    The prototype (e.g. .fn in the jQuery world is used when you want to add methods to actual jQuery objects.

    When you do this $('#myDiv').myFunction() is a member function of a live jQuery object. You can refer to this inside the myFunction() implementation and it will be a jQuery object that, in this case holds the DOM object that corresponds to id="myDiv". If you return the jQuery object from your method, you can also use chaining.

    Which to Use?

    If your code operates on a jQuery object, then it should be a method on a live jQuery object and it should access this to get at the jQuery object instance data and you should declare it as $.fn.myFunction = function() {}; and call it as$(‘#myDiv’).myFunction()`.

    If you code does not operate on jQuery object and is just a utility function that you call and it doesn’t always operate on a jQuery object, then, you should declare it as $.myFunction = function() {}; and you should call it as $.myFunction().

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

Sidebar

Related Questions

Can you do this? var myFunction = function(){ $(function(){ // jQuery code here $(#myField).hide();
I have some code like this: var content = document.getElementById('myDivId'); function MyFunction() { alert(content.style.height);
Suppose I have the following code function myFunction(param, callback) { ... if (err) {
I have this.... function MyFunction() { var myVar = I think I am encapsulated;
private function myFunction(numIn:Number){ trace (numIn + numIn); } var plan:Object = { theFunctionName: myFunction
myInterval = setInterval(function(){ MyFunction(); },50); function MyFunction() { //Can I call clearInterval(myInterval); in here?
Modal Dialog Options function Myfunction() { var clientContext = new SP.ClientContext.get_current(); var options =
Say I have the following code: $('#someid').click(function(event) { myFunction(event); }); function myFunction(event) { //
Is there a way to make this code work without a Warning? function myFunction($value,
I have one function: function myFunction(){ var id = $(this).attr('id'); } Now sometimes, myFunction

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.