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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:28:55+00:00 2026-05-14T22:28:55+00:00

In jQuery, I have seen both the following ways of defining a jQuery function:

  • 0

In jQuery, I have seen both the following ways of defining a jQuery function:

$.fn.CustomAlert = function() {
  alert('boo!');
};

$.CustomAlert = function() {
  alert('boo!');
};

I understand that they are attached to the jQuery object (or $), but what is the difference between the two? When should I use one or the other?

Thanks.

  • 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-14T22:28:56+00:00Added an answer on May 14, 2026 at 10:28 pm

    I’m sure this question has been asked several times before, but I can’t find the link.

    $.fn points to the jQuery.prototype. Any methods or properties you add to it become available to all instance of the jQuery wrapped objects.

    $.something adds a property or function to the jQuery object itself.

    Use the first form $.fn.something when you’re dealing with DOM elements on the page, and your plugin does something to the elements. When the plugin has nothing to do with the DOM elements, use the other form $.something.

    For example, if you had a logger function, it doesn’t make much sense to use it with DOM elements as in:

    $("p > span").log();
    

    For this case, you’d simply add the log method to the jQuery object iself:

    jQuery.log = function(message) {
        // log somewhere
    };
    
    $.log("much better");
    

    However, when dealing with elements, you would want to use the other form. For example, if you had a graphing plugin (plotGraph) that takes data from a <table> and generates a graph – you would use the $.fn.* form.

    $.fn.plotGraph = function() {
        // read the table data and generate a graph
    };
    
    $("#someTable").plotGraph();
    

    On a related note, suppose you had a plugin which could be used either with elements or standalone, and you want to access it as $.myPlugin or $("<selector>").myPlugin(), you can reuse the same function for both.

    Say we want a custom alert where the date is prepended to each alert message. When used as a standalone function, we pass it the message as an argument, and when used with elements, it uses the text of the element as the message:

    (function($) {
        function myAlert(message) {
            alert(new Date().toUTCString() + " - " + message);
        }
    
        $.myAlert = myAlert;
    
        $.fn.myAlert = function() {
            return this.each(function() {
                myAlert($(this).text());
            });
        };
    })(jQuery);
    

    It’s wrapped in a self-executing function so myAlert doesn’t spill out to the global scope. This is an example or reusing functionality between both the plugin forms.

    As theIV mentioned, it is a good practice to return the jQuery wrapped element itself since you wouldn’t want to break chaining.

    Finally, I found similar questions 🙂

    • Difference between jQuery.extend and jQuery.fn.extend?
    • jQuery plugin .fn question
    • jQuery Plugin Authoring: Why do some do jQuery.pluginName and others jQuery.fn.pluginName?
    • in jQuery what is the difference between $.myFunction and $.fn.myFunction?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.