AndyB
AndyB

Reputation: 391

Understanding Javascript Event loop and code execution

I have a problem understanding the Javascript event loop and code execution.

For example, I have a very basic jQuery function like:

$(document).ready(function()
{
$('#button').on('click', function() {
alert("This is a test");
});

Can somebody please explain to me, when the jQuery function gets called?
What happens if the page is loaded, is the .ready() function put on the message queue and gets passed to the event loop when the callback for the function is fired? (In this case, the callback would be the finish loaded page?)

Upvotes: 0

Views: 641

Answers (1)

Nikhil Khullar
Nikhil Khullar

Reputation: 733

The .ready() function gets called when DOM tree has been constructed from your HTML and that's why it is called DOM ready event. After this, the anonymous function above which has the alert gets bound to click event of the element with the ID button . This means that whenever this target is clicked (which has ID button) this function will be called, and hence your alert will show. I hope that helps !

Upvotes: 2

Related Questions