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?
Just defining a function within the closure (function) you pass into
$()doesn’t do anything to put it on the jQueryfnobject. To do that, you have to do it explicitly: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:
…which you might use from DOM ready:
Live example | Live source
Note that the call to it is from a jQuery instance, not directly from
$.fn.