Search code examples
jqueryajaxfunctionformsdollar-sign

What does putting a javascript function inside a '$()' do?


I'm learning some AJAX right now and the jQuery function that is being used to submit the form is wrapped inside $( function() { } ) as so. What does this exactly do?

$(function() {
    $('.error').hide();
    $(".button").click(function() {
        // validate and process form here
    }
});

Solution

  • This is a shortcut provided by jQuery for running code on page ready. It is equivalent to:

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

    jQuery will call this function when the page is ready to be manipulated.

    Docs