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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:38:42+00:00 2026-05-11T22:38:42+00:00

My goal is to be able to call functions that are inside my JQuery

  • 0

My goal is to be able to call functions that are inside my JQuery plugin.

What is the correct syntax?

For example, this does not work:

<a href="#" id="click_me">Click Me</a>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
(function($) { 
    $.fn.foo = function(options) {
        do_stuff = function(){
            console.log("hello world!"); // works
            do_other_stuff = function(){
            alert("who are you?");
            }
        } // function
    } // function
})(jQuery);

$("body").foo();

$("#click_me").click(function(){
$.fn.foo.do_stuff.do_other_stuff(); // doesn't work
});

</script>
  • 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-11T22:38:43+00:00Added an answer on May 11, 2026 at 10:38 pm

    when you assign functions to variables without the var keyword they either overwrite the local variable of that name or the are added to the global namespace. (so your do_stuff is a global function, which is not what you want)

    one way to do what you want is to explicitly give where you want your function to reside.

    (function($) { 
        $.fn.foo = function(options) {
            // whatever $().foo() should do
        };
    
        $.fn.foo.do_stuff = function() {
            console.log("hello world!");
        };
    
        $.fn.foo.do_stuff.do_other_stuff = function(){
            alert("who are you?");
        };
    })(jQuery);
    

    Edit:

    This works because all functions in javascript are objects, which means you can assign values to any arbitrary property.

    If you want to access the variables of other functions you can move the definitions inside of the other ones like:

    $.fn.foo.do_stuff = function() {
        console.log("hello world!");
        $.fn.foo.do_stuff.do_other_stuff = function(){
            alert("who are you?");
        };
    };
    

    but this will mean the function is only defined once you run the other function, and that each time you run the function it will overwrite the last definition.

    Possibly a more ideal solution would be to have each function return an object containing the nested function like so:

    (function($) { 
        $.fn.foo = function(options) {
            // whatever $().foo() should do
    
            var do_stuff = function(do_stuff_args) {
                console.log("hello world!");
                // do stuff with options and do_stuff_args
    
                var do_other_stuff = function(other_args) {
                    alert("who are you?");
                    // here you have access to options, do_stuff_args, and other_args
                };
    
                return {
                    do_other_stuff: do_other_stuff
                };
            };
    
            return {
                do_stuff: do_stuff
            }
        };
    })(jQuery);
    

    and call it using

    foo().do_stuff(some_options).do_other_stuff(other_options);
    

    or

    var a = foo(stuff).do_stuff(some_options);
    a.do_other_stuff(foo);
    a.do_other_stuff(bar);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to be able to have the user select a range of
The goal is to be able to add a PDF file to the assets
I am working on this small project where I'd like to generate the call
So I am using a jquery plug in that allows me to change the
I have a Sybase ASE server that I'm able to connect to with ASE
I want to be able to call through the API to get an array
I'm trying to use jQuery-jsonp plugin with jQuery Deferred Objects but I'm having no
Does anyone know a good tool that would give me x86 instruction execution count.
I am in need of creating a dynamically extend-able class in C#. The goal
I'm trying to create a C# class that can call the same methods on

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.