views:

142

answers:

4

How can I call the Jquery Function From other javaScript Function (Not from jquery function) i.e I have written some Jquery code like below

 $(document).ready(function()  
 {  

   function func1(){
    // Do Something.
    }  
 });

Now I want to call the func1() function from other JavaScript Function

i.e Say an Example

function callJqueryFunction(){
 **func1();**  
}

The above javaScript function calling not work

but If do the same code inside a

$(document).ready(function()  
 {  

   function func1(){
    // Do Something.
    }  

   **func1();**    
 });

Its Work fine.

So what can I do for call the function which is inside a Jquery code format.

+1  A: 

Isn't func1 scoped inside that ready function? If you declare func1 outside of ready it should be available to other javascript code just as any other function.

So:

$(document).ready(function()  
 {  
   func1();
 });

function func1()
{
// Do something
}

function SomeOtherJavascriptFunction()
{
    func1();
}
DavidGouge
+3  A: 

this has nothing to do with jquery in general, it's just a scoping issue

function foo() 
{
   function bar() {
        ....
   }

   bar() // Ok
}

bar() // Not OK

function 'bar' is "local" in foo and is not visible outside of it. if you want a function to be used in different contexts, declare it globally.

stereofrog
+1  A: 

The function func1 is defined in the scope of the parent function. If you don't need this, you can simply move the definition outside (I expect in case of $(document).ready you don't really need it). Otherwise you will need to pass/store the function reference somewhere, and use that to call it.

Lukáš Lalinský
+1  A: 

You can do something like this

var funcToCall;

$(document).ready(function()  
 {  

   funcToCall = function func1(){
    // Do Something.
    }  
 });


funcToCall();
rahul