Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery function inside a function

Is it possible to have a function within another function like so?

function foo() {

    // do something

    function bar() {

        // do something

    }

    bar();

}

foo();
like image 757
daryl Avatar asked Jul 21 '11 22:07

daryl


People also ask

How do you call a function within a function in jQuery?

function someFunction() { //do stuff } $(document). ready(function(){ //Load City by State $('#billing_state_id'). live('change', someFunction); $('#click_me'). live('click', function() { //do something someFunction(); }); });

Can you put jQuery inside a JavaScript function?

You can directly write your jQuery code within a JavaScript function.

How can call onclick function inside another function in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method.

What is $( function () in jQuery?

So a jQuery function, which is prefixed with the $ or the word jQuery generally is called from within that method. $(document). ready(function() { // Assign all list items on the page to be the color red.


2 Answers

Yes you can have it like that. bar won't be visible to anyone outside foo.

And you can call bar inside foo as:

function foo() {

    // do something

    function bar() {

        // do something

    }
    bar();

}
like image 200
Mrchief Avatar answered Oct 03 '22 01:10

Mrchief


Yes, you can.
Or you can do this,

function foo(){

    (function(){

        //do something here

    })()

}

Or this,

function foo(){

    var bar=function(){

        //do something here

    }

}

Or you want the function "bar" to be universal,

function foo(){

    window.bar=function(){

        //something here

    }

}

Hop this helps you.

like image 24
Derek 朕會功夫 Avatar answered Oct 03 '22 00:10

Derek 朕會功夫