288

Sometimes I make a function and call the function later.

Example:

function example { alert('example'); }
example(); // <-- Then call it later

Somehow, some functions cannot be called. I have to call those functions inside:

$(function() { });

What do $(function() {}); and (function() { }); mean, and what's the difference/purpose of these?

rogerrw
1,8743 gold badges15 silver badges19 bronze badges
asked Oct 4, 2011 at 1:33
0

6 Answers 6

421
$(function() { ... });

is just jQuery short-hand for

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

What it's designed to do (amongst other things) is ensure that your function is called once all the DOM elements of the page are ready to be used.

However, I don't think that's the problem you're having - can you clarify what you mean by 'Somehow, some functions are cannot be called and I have to call those function inside' ? Maybe post some code to show what's not working as expected ?

Edit: Re-reading your question, it could be that your function is running before the page has finished loaded, and therefore won't execute properly; putting it in $(function) would indeed fix that!

answered Oct 4, 2011 at 1:37
Sign up to request clarification or add additional context in comments.

8 Comments

what if $(function() { }); is already in $(document).ready() ?
the function does not work without $(function() though it is already in $(document)ready().
Good question! I believe it should just work, as jQuery knows you're in the right place, but it's certainly extraneous; if you're inside .ready() you can just call your function as normal. If it's not working, then post a sample, or even better - try and make a fiddle as jsfiddle.net.
@JeongWooChang Do it like so (function () { ... })();. You have to add () to invoke your function.
"Experienced developers sometimes use the shorthand $() for $( document ).ready(). If you are writing code that people who aren't experienced with jQuery may see, it's best to use the long form." - learn.jquery.com
|
21

The following is a jQuery function call:

$(...);

Which is the "jQuery function." $ is a function, and $(...) is you calling that function.

The first parameter you've supplied is the following:

function() {}

The parameter is a function that you specified, and the $ function will call the supplied method when the DOM finishes loading.

Nathan Arthur
9,2977 gold badges65 silver badges83 bronze badges
answered Oct 4, 2011 at 1:38

Comments

14

It's just shorthand for $(document).ready(), as in:

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

Sometimes you have to use it because your function is running before the DOM finishes loading.

Everything is explained here: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

answered Oct 4, 2011 at 1:37

Comments

12

Some Theory

$ is the name of a function like any other name you give to a function. Anyone can create a function in JavaScript and name it $ as shown below:

$ = function() { 
 alert('I am in the $ function');
 }

JQuery is a very famous JavaScript library and they have decided to put their entire framework inside a function named jQuery. To make it easier for people to use the framework and reduce typing the whole word jQuery every single time they want to call the function, they have also created an alias for it. That alias is $. Therefore $ is the name of a function. Within the jQuery source code, you can see this yourself:

window.jQuery = window.$ = jQuery;

Answer To Your Question

So what is $(function() { });?

Now that you know that $ is the name of the function, if you are using the jQuery library, then you are calling the function named $ and passing the argument function() {} into it. The jQuery library will call the function at the appropriate time. When is the appropriate time? According to jQuery documentation, the appropriate time is once all the DOM elements of the page are ready to be used.

The other way to accomplish this is like this:

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

As you can see this is more verbose so people prefer $(function() { })

So the reason why some functions cannot be called, as you have noticed, is because those functions do not exist yet. In other words the DOM has not loaded yet. But if you put them inside the function you pass to $ as an argument, the DOM is loaded by then. And thus the function has been created and ready to be used.

Another way to interpret $(function() { }) is like this:

Hey $ or jQuery, can you please call this function I am passing as an argument once the DOM has loaded?

answered Oct 3, 2020 at 20:45

Comments

6

I think you may be confusing Javascript with jQuery methods. Vanilla or plain Javascript is something like:

function example() {
}

A function of that nature can be called at any time, anywhere.

jQuery (a library built on Javascript) has built in functions that generally required the DOM to be fully rendered before being called. The syntax for when this is completed is:

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

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. 
 // This does not work until AFTER the entire DOM is "ready", hence the $(document).ready()
 $('li').css('color', 'red'); 
});

The pseudo-code for that block is:

When the document object model $(document) is ready .ready(), call the following function function() { }. In that function, check for all <li>'s on the page $('li') and using the jQuery method .CSS() to set the CSS property "color" to the value "red" .css('color', 'red');

answered Oct 4, 2011 at 1:42

Comments

2

This is a shortcut for $(document).ready(), which is executed when the browser has finished loading the page (meaning here, "when the DOM is available"). See http://www.learningjquery.com/2006/09/introducing-document-ready. If you are trying to call example() before the browser has finished loading the page, it may not work.

answered Oct 4, 2011 at 1:37

1 Comment

"finished loading the page" is inaccurate and misleading.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.