5

I have some jQuery code:

$(function(){ 
function someFunc(){
    /*do something*/
};
.............   
});

And now I want call someFunc from JavaScript like below:

function test(){    
$.fn.someFunc();
}

But it doesn't work! I've seen an article how to call function in jquery from javascript. Is it possible?

1
  • someFunc is just a function defined inside another function... it has nothing to do with jQuery. Commented Mar 20, 2012 at 22:32

3 Answers 3

5

Just defining a function within the closure (function) you pass into $() doesn't do anything to put it on the jQuery fn object. To do that, you have to do it explicitly:

$.fn.someFunc = someFunc;

Typically, you wouldn't do this within a callback on $(), you'd do it earlier. Plug-ins do this to add functionality, which should already be present prior to DOM ready.

By way of example, here's a silly little plug-in that turns text green:

(function($) {
  $.fn.green = green;
  function green() {
    return this.css("color", "green");
  }
})(jQuery);

...which you might use from DOM ready:

jQuery(function($) {

  $(".foo").green();

});

Live example | Live source

Note that the call to it is from a jQuery instance, not directly from $.fn.

Sign up to request clarification or add additional context in comments.

Comments

1

You function someFunc is not a "jQuery function", it is a function defined inside a (jQuery) DOMContentLoaded event handler.

You can't access it from outside because it is out of scope. Just declare it outside the load handler, and it will be accessible:

$(function(){ 
    // nothing here , but you can call someFunc() if you want
});

function someFunc(){
    /*do something*/
};

function test(){    
    someFunc(); // works!
}

Comments

1

In jquery

$.fn.someFunc = myFunc;
function myFunc()
{
    // Code here
}

is similar to plain javascript's

function myObj()
{
    // Code here
}
var obj= new myObj();
obj.prototype.someFunc=myFunc;
function myFunc()
{
    // Code here
}

It's just a way to add a new property to an object (prototype inheritance) and jquery's fn is just an alias of javascript's prototype. To call my 'sumFunc' method that I have added earlier in to jquery object using

$.fn.someFunc = myFunc;

I can do

$('someElement').someFunc();

and this line will call taht function and do something to the 'someElement' (though I did nothing), hope it's clear enough. Look at T.J. Crowder's answer, it'll give you some more understanding.

5 Comments

"... jquery's fn is just an alias of javascript's prototype..." You mean jQuery.prototype (which is also, bizarrely, referenced from jQuery.fn.init.prototype because jQuery uses new jQuery.fn.init to create instances).
Yes, take a look at this stackoverflow.com/questions/1755080/…, am I wrong with my understanding ?
@Sheikh Heera: I don't understand your question. All that link does is reiterate (incompletely) what I said above. Note that "javascript" != "jquery".
@ T.J. Crowder, I asked you whether I'm wrong or right at I said "jquery's fn is just an alias of javascript's prototype", is my understanding wrong ?
@Sheikh Heera: "javascript's prototype" of what? The phrase doesn't make any sense to me just on its own. That's why I said "JavaScript != jQuery"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.