JavaScript is a traditional language that can be used to create dynamic and interactive websites. jQuery is a JavaScript library that simplifies how you interact with the DOM. Its key features include event handling, animation, and Ajax interactions. jQuery has become one of the most popular JavaScript libraries for web development as it makes common tasks simpler and faster to complete.Â
$()
stands for “jQuery function”, which means it will activate when a user’s browser loads your webpage. This function will then call all other functions within it automatically, so they can do their jobs too! The code: $(document).ready(function()) checks if there are any events or DOM elements ready before running any other scripts or stylesheets.Â
What Does $(function() {} ); Do?
$(function() { … });
is a short-hand for $(document).ready(function() { … });
in jQuery
$(function() {} ); is a JavaScript function that allows you to define an anonymous JavaScript function, or “lambda” in programming terms. This function will execute immediately when the page loads, so it’s perfect for those small snippets of code you need on every page like adding event listeners or handling form submissions.
We can convert:
$(function() {
alert("Hi there!");
});
to this:
$(document).ready(function(){
alert("Hi there!");
});
All Ways to Apply jQuery Document Ready
$(document).ready(function() { ... });
$(function() { ... });
jQuery(document).ready(function() { ... });
jQuery(function() { ... });