What is the use of writing a jQuery function like so...
$(function myFunction() {
...
});
What i mean is why wrap the function in $
5 Different Ways to Declare Functions in jQuery.
In jQuery we can assign function in custom namespace. We can add or remove the function from namespace. This mechanism provide the functionality of storing the related function in a namespace.
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)
Answer: Use the syntax $. fn. myFunction=function(){} The syntax for defining a function in jQuery is little bit different from the JavaScript.
I think that you mean like this:
$(function() {
...
});
This is shorthand for:
$(document).ready(function() {
...
});
What it does is registering a handler for the ready
event, so the code in the function will be run as soon as the document has loaded.
It's a shortcut for
$(document).ready(function myFunction() {
...
});
See http://api.jquery.com/ready/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With