jQuery .ready() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Document ready

What You’ll Learn

The jQuery .ready() method runs your code when the DOM is parsed and safe to manipulate. This tutorial covers the recommended $(function(){}) shorthand, handler ordering, comparisons with DOMContentLoaded and window load, the jQuery.noConflict() pattern, and how .ready() relates to jQuery.ready.

01

$(fn)

Shorthand

02

DOM safe

Parse done

03

Queue

In order

04

Late bind

Still runs

05

Not load

Before imgs

06

3.0+

$(fn) only

Introduction

jQuery scripts usually need HTML elements to exist before they can select nodes, attach events, or change the page. If your script runs in the <head> before the body markup appears, $("#menu") finds nothing.

The .ready( handler ) method solves that by queueing your function until jQuery detects the document is ready — the same timing as the browser’s DOMContentLoaded event, but with jQuery’s reliable late-registration behavior.

Most developers never write .ready explicitly. They use the shorthand $(function(){ ... }) — the pattern you will see in virtually every jQuery tutorial and plugin.

Understanding the .ready() Method

.ready( handler ) accepts one argument: a function with no parameters. jQuery calls it when the DOM is ready. The method returns the same jQuery object it was called on (usually jQuery(document) or the collection from $(fn)), so you can chain if needed.

Handlers run in the order they were registered. Since jQuery 3.0, if one handler throws an error, later handlers still execute — jQuery isolates exceptions so one bad plugin does not break the entire page.

💡
Beginner Tip

Put page initialization inside $(function(){ ... }). Use $(window).on("load", fn) only when you need images or other assets fully downloaded first.

📝 Syntax

Register a callback for document ready:

jQuery
.ready( handler )

Recommended forms (jQuery 3.0+)

jQuery
// Recommended shorthand
$(function () {
  // Handler for .ready() called.
});

// Equivalent long form
$(document).ready(function () {
  // Same timing as above.
});

Parameters

  • handler (Function) — callback executed when the DOM is ready.

Return value

  • jQuery object — the collection .ready() was invoked on.

Deprecated equivalents (avoid in new code)

  • $("img").ready(fn) — misleading; does not wait for images.
  • $("document").ready(fn) — selects nothing; still worked historically.
  • $(document).on("ready", fn) — removed in jQuery 3.0.

⚡ Quick Reference

ExpressionMeaning
$(fn)Recommended ready shorthand (3.0+)
$(document).ready(fn)Explicit equivalent
$(window).on("load", fn)All assets loaded — different event
document.addEventListener("DOMContentLoaded", fn)Native — misses late registration
$.when($.ready).then(fn)Promise-style — see jQuery.ready
Multiple $(fn) callsRun in registration order

📋 .ready() vs DOMContentLoaded vs load

Three timing hooks — pick the one that matches what your code needs.

$(fn)
DOM parsed

Default jQuery init

DOMContentLoaded
native

No late bind

window load
all assets

Images ready

$.ready
thenable

Compose w/ Ajax

Examples Gallery

Examples follow the official jQuery .ready() documentation. Use jQuery 3.7.1 in DevTools or the Try-it links.

📚 Getting Started

Official ready demo — update text when the DOM is loaded.

Example 1 — Official Ready Demo

Change paragraph text when the DOM is ready — recommended $(function(){}) form.

jQuery
$(function () {
  $("p").text("The DOM is now loaded and can be manipulated.");
});
Try It Yourself

How It Works

jQuery waits until elements exist, then runs your callback. The paragraph is in the HTML before the script — by ready time, jQuery can select and update it safely.

📈 Practical Patterns

Long form syntax, handler queues, noConflict, and load timing.

Example 2 — $(document).ready() Long Form

Explicit document binding — identical timing to the shorthand.

jQuery
$(document).ready(function () {
  // Handler for .ready() called.
  $("#status").text("Ready via $(document).ready()");
});
Try It Yourself

How It Works

Both forms queue on the same internal ready list. Prefer $(fn) in new code — shorter and the jQuery 3.0 recommended idiom.

Example 3 — Multiple Handlers Run in Order

Successive $(fn) calls execute FIFO when DOM is ready.

jQuery
$(function () {
  console.log("First ready handler");
});

$(function () {
  console.log("Second ready handler");
});

$(document).ready(function () {
  console.log("Third ready handler");
});
Try It Yourself

How It Works

Plugins and app code can each register their own ready block. jQuery flushes the queue once — order matches registration, which keeps init predictable across files.

Example 4 — Official jQuery.noConflict() Pattern

Ready handler receives $ as first argument when using an aliased jQuery object.

jQuery
var jq2 = jQuery.noConflict();

jq2(function ($) {
  // Code using $ as usual; actual jQuery object is jq2
  $("p").text("Ready with noConflict — $ works inside handler");
});
Try It Yourself

How It Works

When global $ belongs to another library, pass a function to jq2. jQuery injects a local $ parameter scoped to your ready callback — official docs pattern for coexistence.

Example 5 — .ready() vs $(window).on("load")

Ready fires before images finish — use load when you need image dimensions.

jQuery
$(function () {
  var img = $("#hero")[0];
  console.log("Ready — img width:", img.width); // often 0
});

$(window).on("load", function () {
  var img = $("#hero")[0];
  console.log("Load — img width:", img.width);  // natural width
});
Try It Yourself

How It Works

The official docs warn: do not attach a load listener inside ready expecting it to fire if load already passed. For dynamically injected scripts, ready handlers still run; window load may have already occurred.

🚀 Common Use Cases

  • Page initialization — bind events, cache DOM references, run plugins.
  • Progressive enhancement — enhance HTML that already rendered without JS.
  • Plugin bootstrapping — each file registers $(fn) independently.
  • Form validation UI — attach submit handlers after inputs exist.
  • Ajax-heavy SPAs (legacy) — first paint init; dynamic content uses delegated events.
  • Coexist with other librariesnoConflict ready pattern.

🧠 How .ready() Queues Your Callback

1

Register handler

$(fn) pushes function onto jQuery’s ready list.

Queue
2

DOM parses

Browser builds tree; jQuery listens for ready signal.

Parse
3

Flush queue

Handlers run in order; jQuery.ready thenable resolves.

Run
4

Manipulate DOM

Select elements, wire UI, start widgets — before full page load.

📝 Notes

  • Available since jQuery 1.0 — core API in all 3.x releases.
  • Since jQuery 3.0, only $(handler) shorthand is recommended for new code.
  • $(document).on("ready", fn) removed in jQuery 3.0.
  • Late registration still executes — unlike native DOMContentLoaded.
  • Related: jQuery.ready, jQuery.holdReady().
  • Modern vanilla alternative: DOMContentLoaded or defer on script tags.

Browser Support

.ready() works wherever jQuery runs. jQuery normalizes ready timing across browsers, including legacy IE behavior before DOMContentLoaded was universal.

jQuery 1.0+

jQuery .ready()

Supported in jQuery 1.x, 2.x, and 3.x. Native DOMContentLoaded is available in all modern browsers for non-jQuery code.

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.ready() Universal

Bottom line: Default entry point for jQuery pages — wrap DOM work in $(function(){}) unless you need window load or explicit promise composition.

Conclusion

The jQuery .ready() method is how almost every jQuery page starts — queue a function that runs when the DOM is safe to touch. Use $(function(){}) for everyday work; reach for $(window).on("load") when assets must finish first.

For promise-style composition with Ajax, continue to jQuery.ready. For advanced legacy loaders, see jQuery.holdReady().

💡 Best Practices

✅ Do

  • Wrap DOM code in $(function(){})
  • Use delegated events for content loaded later via Ajax
  • Keep ready handlers focused — split large inits into functions
  • Use $(window).on("load") for image-dependent layout
  • Use jq2(function($){}) after noConflict()

❌ Don’t

  • Manipulate DOM before elements exist without ready
  • Use deprecated $("img").ready(fn) in new code
  • Assume ready waits for images or fonts
  • Attach window load inside ready expecting it always fires
  • Put heavy blocking work in ready without deferring

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery .ready()

Run code when the DOM is parsed — not when all assets load.

5
Core concepts
DOM 02

Parsed

Not load

Timing
1→3 03

Order

FIFO queue

Queue
04

Late

Still runs

Bind
3.0 05

$(fn)

Recommended

Modern

❓ Frequently Asked Questions

.ready(handler) registers a function to run when the DOM is safe to manipulate — after HTML is parsed but before all images and assets finish loading. Returns the jQuery collection for chaining. Available since jQuery 1.0.
They are equivalent. $(fn) is shorthand for passing a function to jQuery, which internally binds it as a document ready handler. Since jQuery 3.0, $(handler) is the only recommended syntax — other forms like $('img').ready(fn) still work but are deprecated.
Both fire when the DOM is parsed. jQuery's .ready() still runs your handler if you register it after DOMContentLoaded already fired — native addEventListener('DOMContentLoaded') added too late never runs. That late-binding behavior is a key jQuery advantage.
Use .ready() (or $(fn)) for DOM setup, event handlers, and plugins. Use $(window).on('load', fn) when you need fully loaded assets — image dimensions, fonts, or layout that depends on resources. DOM ready always happens before window load.
.ready(handler) queues callbacks on jQuery's internal ready system. jQuery.ready is the thenable property that resolves at the same moment — use $.when($.ready, ajax) for promise composition; use $(fn) for everyday page initialization.
Yes. Each .ready() or $(fn) call adds another handler. They execute in registration order when the DOM becomes ready. Since jQuery 3.0, an exception in one handler does not block subsequent handlers from running.
Did you know?

Passing a function directly to jQuery — $(fn) — is not magic syntax. jQuery checks whether the first argument is a function and, if so, treats it as a ready handler rather than a selector string. That is why $(function(){}) works even though no elements are selected. jQuery 3.0 narrowed official recommendations to this form because variants like $("img").ready(fn) confused beginners into thinking ready waited for images to load.

Next: jQuery.ready Property

Compose document timing with Ajax using the jQuery.ready thenable.

jQuery.ready tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful